Field Shorthand Syntax

If you already have variables with the right names, then you can create the struct using a shorthand:

#[derive(Debug)]
struct Person {
    name: String,
    age: u8,
}

impl Person {
    fn new(name: String, age: u8) -> Person {
        Person { name, age }
    }
}

fn main() {
    let peter = Person::new(String::from("Peter"), 27);
    println!("{peter:?}");
}

The new function could be written using Self as a type, as it is interchangeable with the struct type name

impl Person {
    fn new(name: String, age: u8) -> Self {
        Self { name, age }
    }
}