I can't call Foo::new(words).split_first() in the following code
fn main() { let words = "Sometimes think, the greatest sorrow than older"; /* let foo = Foo::new(words); let first = foo.split_first(); */ let first = Foo::new(words).split_first(); println!("{}", first); } struct Foo<'a> { part: &'a str, } impl<'a> Foo<'a> { fn split_first(&'a self) -> &'a str { self.part.split(',').next().expect("Could not find a ','") } fn new(s: &'a str) -> Self { Foo { part: s } } } the compiler will give me an error message
error[E0716]: temporary value dropped while borrowed --> src/main.rs:8:17 | 8 | let first = Foo::new(words).split_first(); | ^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement | | | creates a temporary which is freed while still in use 9 | 10 | println!("{}", first); | ----- borrow later used here | = note: consider using a `let` binding to create a longer lived value If I bind the value of Foo::new(words) first, then call the split_first method there is no problem.
These two methods of calling should intuitively be the same but are somehow different.
temporary value is freed at the end of this statement,creates a temporary which is freed while still in useandborrow later used here. I think the error message is pretty clear. If not, please tell us, what you are you understanding.