What is wrong in the following code -

#[derive(Debug)]
struct Rect {
    width: u32,
    height: u32,
    name: &String
}

Screenshot 2025-05-16 at 7.32.11 PM.png

We need to specify the lifetime of the struct and associate it to the lifetime of the individual references

#[derive(Debug)]
struct Rect<'a> {
    width: u32,
    height: u32,
    name: &'a String
}

fn main() {
    println!("Hello, world!");
    let name = String::from("rect");
    let rect = Rect { width: 10, height: 10, name: &name };
    println!("{:?}", rect);
}

Struct that requires a struct that requires a lifetime

struct Shape<'a> {
    rect1: Rect<'a>,
    rect2: Rect<'a>
}