0

I can't understand what's going on here:

struct MyStruct<'a> {
  field1: i32,
  field2: &'a str,
}

impl<'a> MyStruct<'a> {
    fn foo(var1: i32, var2: i32, var3: &str) -> &'a str {
      &format!("{} {} {} fdsfdsfd", var1, var2, var3)
    }
}

fn main() {
}

Should I somehow return str with lifetime 'a from foo? If not, how to fix it?

imatahi
  • 511
  • 1
  • 7
  • 15

1 Answers1

3

You’re creating a string and then trying to return a reference to it, but the string that you’re returning a reference to is not stored anywhere. You should return the String itself.

Chris Morgan
  • 79,487
  • 22
  • 198
  • 207