(done) 速通 rustlings(8) 函数

RUST 函数定义中,参数必须显性声明类型

rust 复制代码
// The type of function arguments must be annotated.
// Added the type annotation `u64`.
fn call_me(num: u64) {
    for i in 0..num {
        println!("Ring! Call number {}", i + 1);
    }
}

fn main() {
    call_me(3);
}

函数的最后一个语句的变量就是返回值。

函数返回值类型必须显式声明
注意:作为返回值的最后一个语句末尾不能带分号!

rust 复制代码
fn is_even(num: i64) -> bool {
    num % 2 == 0
}

// The return type must always be annotated.
fn sale_price(price: i64) -> i64 {
    if is_even(price) {
        price - 10
    } else {
        price - 3
    }
}

fn main() {
    let original_price = 51;
    println!("Your sale price is {}", sale_price(original_price));
}
相关推荐
Ramble_Naylor18 小时前
错误是一种值:Result、? 与 panic!
rust
柯南466818 小时前
【AI开发之Rust】第 10 课:Cargo 工程化 —— 模块、依赖与测试(基础篇收尾)
rust·编程语言
qq_4523962318 小时前
第九篇:《并发编程:线程、Channel 与共享状态》
rust
Source.Liu19 小时前
【A11】 labelprinter(Tauri 版)新建步骤
windows·rust
孙启超20 小时前
【AI开发之Rust】第 8 课:泛型、trait 与 trait 对象
开发语言·后端·rust
蓝宝石的傻话20 小时前
MiBee Eye Notebook:把闲置笔记本变成一台带麦克风的网络摄像头
数码相机·rust
小灰灰搞电子21 小时前
Rust Once 、OnceLock、LazyLock 一次性初始化详解
开发语言·后端·rust
qq_452396231 天前
第八篇:《智能指针与内部可变性:Rust 的进阶内存管理》
rust
Amos_Web1 天前
Rspack 源码解析(七):Module、Chunk 与加载树优化
前端·rust·源码阅读