将分割映射到修剪每个条目

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

Map the split to trim each entry

问题

I need to write a function that takes a string as an input, splits it by the line break character and trims all the redundant break characters in each entry of the split. I've come up with the following:

fn split_and_trim(text: &String) {
    let lines = text.split('\n').map(|l| String::from(l).trim());
    println!("{:?}", lines)
}

But this code returns me the following error:

6 |     let lines = text.map(|l| String::from(l).trim());
  |                    ---------------^^^^^^^
  |                    |
  |                    returns a reference to data owned by the current function
  |                    temporary value created here

Trying to rewrite it the following way:

let lines: Vec<String> = text.split('\n').map(|l| String::from(l).trim()).collect();

Returns another error:

value of type `Vec<String>` cannot be built from `std::iter::Iterator<Item=&str>`

What would be the correct way of achieving this goal (splitting the string and trimming each element)? Thanks in advance!

英文:

I need to write a function that takes a string as an input, splits it by the line break character and trims all the redundant break characters in each entry of the split. I've come up with the following:

fn split_and_trim(text: &amp;String) {
    let lines - text.split(&#39;\n&#39;).map(|l| String::from(l).trim());
    println!(&quot;{:?}&quot;, lines)
}

But this code returns me the following error:

6 |     let lines = text.map(|l| String::from(l).trim());
  |                                   ---------------^^^^^^^
  |                                   |
  |                                   returns a reference to data owned by the current function
  |                                   temporary value created here

Trying to rewrite it the following way:

let lines: Vec&lt;String&gt; = text.split(&#39;\n&#39;).map(|l| String::from(l).trim()).collect();

Returns another error:

value of type `Vec&lt;String&gt;` cannot be built from `std::iter::Iterator&lt;Item=&amp;str&gt;`

What would be the correct way of achieving this goal (splitting the string and trimming the each element)? Thanks in advance!

答案1

得分: 2

You don't need to create new Strings here, since trim only needs slices.

fn split_and_trim(text: &str) {
    let lines: Vec<&str> = text.split('\n').map(|l| l.trim()).collect();
    println!("{:?}", lines)
}

You should also take &str instead of &String as a function parameter in almost all cases.

If you need Strings, you can convert them after trimming.

英文:

You don't need to create new Strings here, since trim only needs slices. (playground)

fn split_and_trim(text: &amp;str) {
    let lines: Vec&lt;&amp;str&gt; = text.split(&#39;\n&#39;).map(|l| l.trim()).collect();
    println!(&quot;{:?}&quot;, lines)
}

You should also take &amp;str instead of &amp;String as a function parameter in almost all cases.

If you need Strings, you can convert them after trimming.

huangapple
  • 本文由 发表于 2023年3月23日 08:19:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818308.html
匿名

发表评论

匿名网友

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

确定