Given
- A vector of
String
values is set - Iteration clones the
String
values
Requirements
- Create a vector of
&str
values from the givenString
clones
Details
I've tried to convert these in a single loop or map
statement but ran into lifetime issues that I'm confused about. I've written a map
statement that works and one that doesn't but want to gain intuition into what exactly is going on and what the best practice is.
Source Code
#[derive(Debug)]
struct Container<'a> {
items: Vec<&'a str>,
}
fn main() {
let numbers = vec![
String::from("one"),
String::from("two"),
String::from("three"),
];
// --- using two closures with map --- WORKS ---
let strings: Vec<String> = numbers
.iter()
.map(|number| {
let item = String::from(number);
item.clone()
})
.collect();
let items: Vec<&str> = strings.iter().map(|string| string.as_str()).collect();
println!("{:?}", Container { items });
// --- using single closures with map --- DOESN'T WORK ---
let string_items: Vec<&str> = numbers
.iter()
.map(|number| {
let item = String::from(number);
let string = item.clone();
string.as_str()
})
.collect();
println!(
"{:?}",
Container {
items: string_items
}
);
}
error[E0515]: cannot return value referencing local variable `string`
--> src/main.rs:30:13
|
30 | string.as_str()
| ------^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `string` is borrowed here
Question Why does one closure fail while two closures work? What is best practice?