如何将 Vec<&str> 转换为 Vec?

huangapple go评论61阅读模式
英文:

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&lt;String&gt; = line.split_whitespace().collect();
let parts: Vec&lt;String&gt; = map(AsRef::as_ref).line.split_whitespace().collect();
if parts.len() &gt;= 3 {
    let attribute_name = parts[1].to_string();
    let attribute_type = parts[2].to_lowercase();
    let attribute = match attribute_type.as_str() {
        &quot;numeric&quot; =&gt; Attribute::Numeric,
        &quot;date&quot; =&gt; Attribute::Date(parts.get(3).cloned()),
        &quot;string&quot; =&gt; Attribute::String,
        &quot;nominal&quot; =&gt; {
            let nominal_values: Vec&lt;String&gt; = parts[3..]
                .iter()
                .map(|s| s.trim_matches(|c| c == &#39;{&#39; || c == &#39;}&#39; || c == &#39;,&#39;).to_string())
                .collect();
            Attribute::Nominal(nominal_values)
        }

如何将 Vec<&str> 转换为 Vec<String>?

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&lt;String&gt; = line.split_whitespace().map(|v| v.to_string()).collect();

huangapple
  • 本文由 发表于 2023年6月13日 08:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定