演習: エレベーターイベント

エレベーター制御システム内のイベントを表すデータ構造を作成します。 さまざまなイベントを構築するための型と関数は、自分で定義してください。 型を {:?} でフォーマットできるようにするため、#[derive(Debug)] を使用してください。

この演習では、main がエラーなく実行されるように、データ構造を作成して 値を設定するだけで十分です。コースの次のパートでは、これらの構造から データを取り出す方法を扱います。

// 著作権 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0

#![allow(dead_code)]

#[derive(Debug)]
/// An event in the elevator system that the controller must react to.
enum Event {
    // TODO: 必要なバリアントを追加する
}

/// A direction of travel.
#[derive(Debug)]
enum Direction {
    Up,
    Down,
}

/// The car has arrived on the given floor.
fn car_arrived(floor: i32) -> Event {
    todo!()
}

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

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

/// A directional button was pressed in an elevator lobby on the given floor.
fn lobby_call_button_pressed(floor: i32, dir: Direction) -> Event {
    todo!()
}

/// A floor button was pressed in the elevator car.
fn car_floor_button_pressed(floor: i32) -> Event {
    todo!()
}

fn main() {
    println!(
        "A ground floor passenger has pressed the up button: {:?}",
        lobby_call_button_pressed(0, Direction::Up)
    );
    println!("The car has arrived on the ground floor: {:?}", car_arrived(0));
    println!("The car door opened: {:?}", car_door_opened());
    println!(
        "A passenger has pressed the 3rd floor button: {:?}",
        car_floor_button_pressed(3)
    );
    println!("The car door closed: {:?}", car_door_closed());
    println!("The car has arrived on the 3rd floor: {:?}", car_arrived(3));
}
  • 学生が演習の先頭にある #![allow(dead_code)] について質問した場合、これは Event 型に対して行っていることが、それを表示することだけなので必要です。 コンパイラがデッドコードを検査する方法の細かな仕様により、コードが 未使用だと判断されてしまいます。この演習の目的では無視して構いません。