(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"
    }
}
相关推荐
段一凡-华北理工大学6 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
大卫小东(Sheldon)12 小时前
小鹤音形词典:一个用 Rust 写的离线编码查询桌面工具
ai·rust
就业发动机13 小时前
我用 Tauri 做了一个约 1.5 MB 的开源屏幕标注工具:MarkerOn
vue.js·rust
2601_9545267515 小时前
【硬核架构】打破 IT 与 OT 的数据孤岛!基于 Rust 异步协程的工业 IoT 网关重构,兼谈顶级自动化智能仪表厂家选型指南
物联网·架构·rust
white_ant15 小时前
17- 文本统计/处理工具使用指南
rust·个人开发·tauri
独孤留白16 小时前
Rust 类型转换全景指南 —— 从引用转换到序列化
rust
独孤留白16 小时前
从C到Rust:Trait TryFrom TryInto 可能失败的类型转换
rust
雨师@17 小时前
python通过rust编写组件扩展自己的能力
python·rust
程序员爱钓鱼19 小时前
Rust 切片 Slice 详解:安全访问连续数据
前端·后端·rust
花褪残红青杏小1 天前
Rust图像处理第20节-PCA 主成分分析:把图片压缩到 3 个数字
rust·webassembly·图形学