(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));
}
相关推荐
卷无止境3 小时前
用Rust重写:三条清晰的收益曲线
后端·rust
geovindu6 小时前
rust: Simple Factory Pattern(续)
后端·设计模式·rust·简单工厂模式·创建型模式
十万公里通票1 天前
Zed / GPUI 设计觉醒:现代编辑器 Action 机制解读
rust
wflynn1 天前
GitHub 今日推荐|lightcraft:纯 Rust 重写的 RAW 照片开发工具
rust·开源·github·lightroom·art·photography
geovindu1 天前
rust: Flyweight Pattern
开发语言·后端·设计模式·rust·享元模式·结构型模式
golang学习记2 天前
rust开发,选VS Code还是RustRover?
开发语言·后端·rust
滕州市燕猫虎计算机科技工作室个体工商户2 天前
《Rust程序设计》学习笔记二
rust·学习笔记
滕州市燕猫虎计算机科技工作室个体工商户2 天前
《Rust程序设计》学习笔记三
rust·学习笔记·rust程序设计
geovindu2 天前
rust: Facade Pattern
开发语言·后端·设计模式·rust·外观模式
福大大架构师每日一题2 天前
Rust 1.99.0发布:C 可变参数、裸函数、Cargo 配置、Rustdoc 性能与大量兼容性调整全解析
c语言·开发语言·rust