lifetime
Rust Understand method receiver lifetime elision
For an ordinary method with a shared or mutable self reference, omitted output reference lifetimes normally follow the receiver lifetime. This is a useful default when returning a view into an object, not a view into another argument. Expected output (not executed): Open Repair Why: The result borrows ticket rather than hint. Common mistake: Returning hint with this elided signature, even though the result is expected to borrow self.
Formal Syntax & Structure
rust syntaxrust
fn title(&self, hint: &str) -> &strWorking Example
Example Snippetrust
struct Ticket { title: String }impl Ticket { fn title(&self, hint: &str) -> &str { println!("{hint}"); &self.title }}fn main() { let ticket = Ticket { title: String::from("Repair") }; let result; { let hint = String::from("Open"); result = ticket.title(&hint); } println!("{result}");}