英文:
How do I convert a Vec <&str> to Vec <String>?
问题
以下是您要翻译的代码部分:
let parts: Vec<String> = line.split_whitespace().collect();
let parts: Vec<String> = map(AsRef::as_ref).line.split_whitespace().collect();
if parts.len() >= 3 {
let attribute_name = parts[1].to_string();
let attribute_type = parts[2].to_lowercase();
let attribute = match attribute_type.as_str() {
"numeric" => Attribute::Numeric,
"date" => Attribute::Date(parts.get(3).cloned()),
"string" => Attribute::String,
"nominal" => {
let nominal_values: Vec<String> = parts[3..]
.iter()
.map(|s| s.trim_matches(|c| c == '{' || c == '}' || c == ',').to_string())
.collect();
Attribute::Nominal(nominal_values)
}
如果您需要进一步的帮助,请随时提出。
英文:
let parts: Vec<String> = line.split_whitespace().collect();
let parts: Vec<String> = map(AsRef::as_ref).line.split_whitespace().collect();
if parts.len() >= 3 {
let attribute_name = parts[1].to_string();
let attribute_type = parts[2].to_lowercase();
let attribute = match attribute_type.as_str() {
"numeric" => Attribute::Numeric,
"date" => Attribute::Date(parts.get(3).cloned()),
"string" => Attribute::String,
"nominal" => {
let nominal_values: Vec<String> = parts[3..]
.iter()
.map(|s| s.trim_matches(|c| c == '{' || c == '}' || c == ',').to_string())
.collect();
Attribute::Nominal(nominal_values)
}
My goal is to implement an algorithm in Rust for loading and reading .arff files. It reads an .arff file, extract information from header, attributes and instances, then prints it on the output. Each attribute is represented as a pair (name, type) in array attributes, and instances are stored as arrays of attribute values in array instances.
答案1
得分: 1
需要将向量的值映射起来。类似这样:
let parts: Vec<String> = line.split_whitespace().map(|v| v.to_string()).collect();
英文:
You would need to map the values of the vector. Something like this:
let parts: Vec<String> = line.split_whitespace().map(|v| v.to_string()).collect();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论