How do I tell the compiler that one lifetime must outlive another?
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Tokens<'a> {
buffer: String,
list: Vec<Token<'a>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Token<'a> {
term: &'a str,
}
yields
error: lifetime may not live long enough
--> src/pipeline/tokenizers/test.rs:6:5
|
3 | #[derive(Serialize, Deserialize, Debug)]
| ----------- lifetime `'de` defined here
4 | pub struct Tokens<'a> {
| -- lifetime `'a` defined here
5 | buffer: String,
6 | list: Vec<Token<'a>>,
| ^^^^ requires that `'de` must outlive `'a`
|
= help: consider adding the following bound: `'de: 'a`
In the code above, the token.term: &str will always refer to a slice of tokens.buffer. I'm not sure how to specify that relationship. I'm also not sure how to add the requested bound.
Will this even work? If so, what's the magic syntax?