rust-book/advanced_lifetimes/src/lib.rs

27 lines
507 B
Rust

struct Context<'s>(&'s str);
// 's : 'c tells Rust that the 's lifetime is tied to the 'c lifetime
struct Parser<'c, 's : 'c> {
context: &'c Context<'s>,
}
impl<'c, 's> Parser<'c, 's> {
fn parse(&self) -> Result<(), &'s str> {
Err(&self.context.0[1..])
}
}
fn parse_context(context: Context) -> Result<(), &str> {
Parser { context: &context }.parse()
}
struct Ref<'a, T: 'a>(&'a T);
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}