Skip to content

Instantly share code, notes, and snippets.

@yhara
Created January 28, 2019 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhara/20a1f04bf446b9944440551742992e98 to your computer and use it in GitHub Desktop.
Save yhara/20a1f04bf446b9944440551742992e98 to your computer and use it in GitHub Desktop.
struct Parser<'a> {
pub str: String,
pub current_token: Option<&'a str>,
}
impl<'a> Parser<'a> {
pub fn lex(&mut self) {
self.current_token = Some(&self.str[0..1]);
}
}
fn main() {
let parser = Parser{
str: "hello".to_string(),
current_token: None,
};
parser.lex();
}
// error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
// --> b.rs:8:36
// |
// 8 | self.current_token = Some(&self.str[0..1]);
// | ^^^^^^^^^^^^^^
// |
// note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 7:5...
// --> b.rs:7:5
// |
// 7 | / pub fn lex(&mut self) {
// 8 | | self.current_token = Some(&self.str[0..1]);
// 9 | | }
// | |_____^
// note: ...so that reference does not outlive borrowed content
// --> b.rs:8:36
// |
// 8 | self.current_token = Some(&self.str[0..1]);
// | ^^^^^^^^
// note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 6:6...
// --> b.rs:6:6
// |
// 6 | impl<'a> Parser<'a> {
// | ^^
// = note: ...so that the expression is assignable:
// expected std::option::Option<&'a str>
// found std::option::Option<&str>
//
// error: aborting due to previous error
//
// For more information about this error, try `rustc --explain E0495`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment