(done) 速通 rustlings(9) 分支跳转

RUST 中的分支跳转-条件判断不需要加括号

rust 复制代码
fn picky_eater(food: &str) -> &str {
    if food == "strawberry" {
        "Yummy!"
    } else if food == "potato" {
        "I guess I can eat that."
    } else {
        "No thanks!"
    }
}

RUST 作为一种表达式导向的语言,if-else 块作为一个表达式,可以直接赋值给变量:

rust 复制代码
fn animal_habitat(animal: &str) -> &str {
    // TODO: Fix the compiler error in the statement below.
    let identifier = if animal == "crab" {
        1
    } else if animal == "gopher" {
        2
    } else if animal == "snake" {
        3
    } else {
        4
    };

    // Don't change the expression below!
    if identifier == 1 {
        "Beach"
    } else if identifier == 2 {
        "Burrow"
    } else if identifier == 3 {
        "Desert"
    } else {
        "Unknown"
    }
}
相关推荐
数据法师12 小时前
MotrixNext:接棒经典 Motrix,用 Tauri 2+Rust 重构的下一代开源下载神器
重构·rust·开源
卡卡军13 小时前
agmd 1.0 重磅升级——Rust 重写,性能起飞
javascript·rust
codealy20 小时前
Rust 核心理论: 高并发与异步(三)
算法·rust
咸甜适中20 小时前
rust语言学习笔记Trait(七) IntoIterator(由集合创建迭代器)
笔记·学习·rust
本地化文档21 小时前
rust-style-guide-l10n
rust·github·gitcode
樱桃花下的小猫1 天前
腐蚀Rust-EAC 及官方验证关闭教程
服务器·rust·云鸢互联·零门槛一键开服·腐蚀rust服务器
咸甜适中1 天前
rust语言学习笔记Trait(六) FromIterator(由迭代器创建集合)
笔记·学习·rust
小杍随笔1 天前
【Rust + Tauri 2 + TypeScript + Tailwind CSS 4 桌面应用 UI 组件选型深度对比(2026版)】
css·rust·typescript
iuyup2 天前
深度解析 OpenHuman:开源个人 AI 超级智能的 Memory 架构设计
人工智能·rust