Skip to content

Instantly share code, notes, and snippets.

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 jw3126/cc0e75cc8275dede5430ca36917f6be0 to your computer and use it in GitHub Desktop.
Save jw3126/cc0e75cc8275dede5430ca36917f6be0 to your computer and use it in GitHub Desktop.
temporary value does not live long enough
#[derive(Debug)]
struct MyString {
s:String,
}
impl MyString {
fn to_str(&self) -> &str {
&self.s
}
}
fn main() {
let s = MyString {s:"1".to_string()};
let t = s.to_str();
// let s2 = MyString {s:"1".to_string()}.to_str();
// does not work. It seems to be equivalent to
// let s3 = {
// MyString {s:"1".to_string()}
// }.to_str();
println!("{:?}", s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment