To start, consider this Rust program. We introduce an Employee struct, with has 3 fields—an integer, and 2 bool fields.
#[derive(Debug)]
struct Employee {
salary: i32,
hired: bool,
fired: bool
}
fn is_current(employee: &Employee) -> bool {
// Return true if Employee is current.
return !employee.fired &&
employee.hired &&
employee.salary > 0;
}
fn is_executive(employee: &Employee) -> bool {
// Return true if Employee is current, and has high salary.
return is_current(employee) &&
employee.salary > 1000000;
}
fn main() {
// Test the bool return functions.
let employee1 = Employee { salary: 0, hired: false, fired: false };
println!(
"{:?}", employee1);
let result1 = is_current(&employee1);
println!(
"{}", result1);
let employee2 = Employee { salary: 2000000, hired: true, fired: false };
println!(
"{:?}", employee2);
let result2 = is_current(&employee2);
println!(
"{}", result2);
let executive2 = is_executive(&employee2);
println!(
"{}", executive2);
}
Employee { salary: 0, hired: false, fired: false }
false
Employee { salary: 2000000, hired: true, fired: false }
true
true