April 2026
Intermediate
631 pages
16h 20m
English
In this section, we’ll provide some practice questions to help reinforce your learning. The exercise questions cover almost all the concepts introduced in the chapter. Solutions to each exercise will follow in Section 12.5.
Initialize a struct with public and private fieldsAdd the implementation for the new constructor function so that the following program compiles.
#[derive(Debug)]struct Employee { employee_id: u32, pub age: u8, pub name: String,}impl Employee { // Add constructor function here}fn main() { let emp_1 = Employee::new("Alice".to_string());}
Validate struct fields using custom validation logicConsider the following code. Add the body for the new constructor to validate the year field to ensure the ...
Read now
Unlock full access