-
-
Save yhara/20a1f04bf446b9944440551742992e98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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