I want to be able to assign a Chars
iterator to a struct, whose String
was created inside of the struct's "constructor" method. How do I do this?
Code (run it):
use std::str::Chars;
fn main() {
let foo = Foo::new();
}
struct Foo<'a> {
chars: Chars<'a>
}
impl<'a> Foo<'a> {
pub fn new() -> Self {
let s = "value".to_string();
Foo { chars: s.chars() }
}
}
Error:
error: `s` does not live long enough
--> <anon>:13:22
13 |> Foo { chars: s.chars() }
|> ^
note: reference must be valid for the lifetime 'a as defined on the block at 11:25...
--> <anon>:11:26
11 |> pub fn new() -> Self {
|> ^
note: ...but borrowed value is only valid for the block suffix following statement 0 at 12:36
--> <anon>:12:37
12 |> let s = "value".to_string();
|>
(In my real code, the constructor reads text from a file)