Exercise: Car Events

We will create a data structure to represent an event in an car Electronic Unit Control (ECU) system. It is up to you to define the types and functions to construct various events. Use #[derive(Debug)] to allow the types to be formatted with {:?}.

This exercise only requires creating and populating data structures so that main runs without errors. The next part of the course will cover getting data out of these structures.

#[derive(Debug)]
/// An event in the elevator system that the controller must react to.
enum Event {
    // TODO: add required variants
}



/// The push of the break pedal is represented by a floating-point percentage.
type Percentage = f32;

#[derive(Debug)]
/// A user-accessible button.
enum Button {
    VolumenUp,
    VolumenDown,
}

#[derive(Debug)]
/// Door of the car.
enum Door {
    FrontLeft,
    FrontRight,
    BackLeft,
    BackRight,
}



/// The car doors have opened.
fn car_door_opened(door: Door) -> Event {
    todo!()
}

/// The car doors have closed.
fn car_door_closed(door: Door) -> Event {
    todo!()
}

/// The car doors have stopped.
fn car_stopped() -> Event {
    todo!()
}

/// A passenger has pressed the volume up button.
fn passenger_button_volumeup() -> Event {
    todo!()
}

/// The driver has pressed the break pedal.
fn break_pedal_pressed(amount: Percentage) -> Event {
    todo!()
}

fn main() {
    println!("Passenger pressed a button: {:?}", passenger_button_volumeup());

    println!("Driver breaks: {:?}", break_pedal_pressed(0.79));

    println!("The car has stopped: {:?}", car_stopped());

    println!("Door opened: {:?}", car_door_opened(Door::FrontLeft));
    println!("The car door closed: {:?}", car_door_closed(Door::FrontLeft));
}

Additional exercices Rustlings

Do the following exercises in rustlings::

  • structs1
  • structs2
  • structs3
  • enums1
  • enums2
  • enums3