I'd like to figure out how to get this to work:
struct Input<'a>{
words: Vec<&'a str>,
normalized: String,
}
impl Input<'_> {
fn new(str : &str) -> Input{
let normalized : String = str.chars().filter(|c| c.is_alphanumeric()).collect();
let words : Vec<&str> = normalized.split_whitespace().collect();
Input{
words: words,
normalized: normalized,
}
}
}
fn main() {
Input::new("Hello, world!");
}
I know I could turn words into a Vec<String> and avoid all of this, but I feel that it must be possible to have words be a Vec<&str> generated from normalized.
I think I have two problems. First is about lifetime. I need to tell the compiler that words and normalized share the same liftetime.
The other is about borrowing. words borrows normalized which, I think, makes it impossible for me move to my struct and return from the function.
Even if those observations are right, I have no idea how to fix it.