MST

星途 面试题库

面试题:Rust中枚举类型与模式匹配基础应用

请用Rust编写一个枚举类型 `TrafficLight`,它有三个变体:`Red`、`Yellow`、`Green`。然后通过模式匹配,当变体为 `Red` 时输出 "Stop",`Yellow` 时输出 "Prepare to stop",`Green` 时输出 "Go"。
13.8万 热度难度
编程语言Rust

知识考点

AI 面试

面试题答案

一键面试
enum TrafficLight {
    Red,
    Yellow,
    Green,
}

fn main() {
    let light = TrafficLight::Red;
    match light {
        TrafficLight::Red => println!("Stop"),
        TrafficLight::Yellow => println!("Prepare to stop"),
        TrafficLight::Green => println!("Go"),
    }
}