Struct with vector and optional value



examples/struct/struct-with-vector-and-optional-value/src/main.rs
struct Course {
    name: String,
    grades: Vec<i32>,
    final_grade: Option<i32>,
}

fn main() {
    let mut c = Course {
        name: String::from("Programming Rust"),
        grades: vec![78, 80],
        final_grade: None,
    };
    println!("{}", c.name);
    println!("{:?}", c.grades);
    println!("{:?}", c.final_grade);
    // println!("{:?}", c.final_grade.unwrap());
    // thread 'main' panicked at 'called `Option::unwrap()` on a `None` value'

    match c.final_grade {
        Some(value) => println!("Final grade is {value}"),
        None => println!("There is no final grade yet"),
    };

    c.grades.push(100);
    c.final_grade = Some(82);
    println!("{:?}", c.grades);
    println!("{:?}", c.final_grade);
    println!("{:?}", c.final_grade.unwrap());

    match c.final_grade {
        Some(value) => println!("Final grade is {value}"),
        None => println!("There is no final grade yet"),
    };
}

Programming Rust
[78, 80]
None
There is no final grade yet
[78, 80, 100]
Some(82)
82
Final grade is 82