rust - Creating a new &str -
I want to map on a vector of strings so that it can be repeated to other values:
let's v = vec! ["Str1", "str2", "str3", "str4"] race = v.map_in_place (| x | x + "__") / error: binary operation `+` can not be applied to type ` & Amp; Str Note that I do not need Change x , give me a New & Str by adding a new string to x . How can I do this?
Before you x to string before You can do + , like x.to_string () + "__" , but there is another problem with your code:
V type Vec & lt; & Amp; Str & gt; and map_in_place the type of type expects the size (and alignment) of the original size, and the and str size string < / Code>, so it fails in sequence -.
There are several possible changes to this task:
If you really want to use map_in_place , then you v < / Code> can be declared:
let's v = vec! ["Str1" .to_string (), "str2" .to_string (), "str3" .to_string (), "str4" .to_string ()];
Another way is in_iter (). The map (...) will be used. () , which will be immediately free, original vector after new creation.
fn main () {V = VC! ["Str1", "str2", "str3", "str4"]; Let = v.into_iter () Map (| x | x.to_string (+ + "__")) Collect: & lt; Vec & lt; _ & Gt; & Gt; (); Println! ("{}", Res); } Output:
[str1__, str2__, str3__, str4__]
Comments
Post a Comment