(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));
}
相关推荐
busideyang2 小时前
MATLAB vs Rust在嵌入式领域的角色定位
开发语言·matlab·rust
Source.Liu3 小时前
【a11】项目命名笔记:`a11` (合一)
rust·egui
Source.Liu18 小时前
【egui】官方示例 hello_world 完全解析
rust·egui
CS生21 小时前
Rust环境准备
开发语言·后端·rust
沛沛rh451 天前
Rust 中的三个“写手“:print!、format!、write! 的详细区别
开发语言·后端·rust
henreash1 天前
Rust引用借用测试
rust
devmoon1 天前
从 0 到 1 实现两条独立区块链Parachain的跨链通信能力之实操指南
开发语言·rust·区块链·信息与通信·polkadot
devmoon1 天前
区块链 Indexer 全解析:为什么 Web3 应用离不开数据索引器?(Polkadot / Ethereum / Solana 对比与未来展望)
rust·web3·区块链·以太坊·polkadot·solana·indexer
@PHARAOH1 天前
WHAT - SWC Rust-based platform for the Web
开发语言·前端·rust