(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));
}
相关推荐
盒马盒马20 小时前
Rust:迭代器
开发语言·后端·rust
Source.Liu1 天前
【Iced】stream.rs文件
rust·iced
Kapaseker1 天前
精通 Rust 宏 — 包装新类型
rust
飞函安全1 天前
Vite 8.0:Rust.bundle,性能提升10-30倍
开发语言·人工智能·rust
奋斗中的小猩猩2 天前
OpenClaw不安全,Rust写的ZeroClaw给出满意答案
安全·rust·openclaw·小龙虾
海奥华22 天前
Rust初步学习
开发语言·学习·rust
VermouthSp2 天前
上下文切换
linux·rust
小杍随笔2 天前
【Rust 1.94.0 正式发布:数组窗口、Cargo 配置模块化、TOML 1.1 全面升级|开发者必看】
开发语言·后端·rust
敬业小码哥3 天前
记一次:clion使用rust插件配置环境并开发
学习·rust
NGINX开源社区3 天前
NGINX 引入对 ACME 协议的原生支持
nginx·rust