Lifetime in Structs

#[derive(Debug)]
struct MyStruct<'a> {
    
    v: &'a i32,
}

fn main() {

    let m;
    {
        let x = 1;

        m = MyStruct { v: &x };

        println!("m is {:?}", m);
    }

    //Uncommment to see the error
    //println!("m is {:?}", m);
}
  • MyStruct instance can't outlive the reference it holds