(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"
    }
}
相关推荐
Jurio.37 分钟前
开源 Codex Sticky:在终端 Codex CLI 长对话中始终固定底部输入框
linux·rust·github·开源软件·codex·codex cli
阿正的梦工坊3 小时前
【Rust】04-借用、引用与切片
java·数据库·rust
福大大架构师每日一题3 小时前
2026年6月TIOBE编程语言排行榜,Go语言排名第13,Rust语言排名12。关于Rust已进入平台期的报道似乎为时过早。
开发语言·golang·rust
咸甜适中3 小时前
rust语言学习笔记Trait(十六)Error(错误)
笔记·学习·rust
guyoung3 小时前
BoxAgnts 工具系统(4)——Tool Trait 与并发上下文模型
rust·agent·ai编程
techdashen6 小时前
What is maintenance, anyway?
开发语言·后端·rust
阿正的梦工坊6 小时前
【Rust】05-结构体、枚举与模式匹配
java·数据库·rust
星栈6 小时前
Makepad 应用如何读文件、调接口、保存数据
前端·rust
阿正的梦工坊6 小时前
【Rust】10-Cargo、测试与实用开发工作流
java·rust·log4j
Java陈序员7 小时前
一键测算!一款筛选本机可流畅运行的大模型终端工具!
rust·llm