(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】02-变量、不可变性与基础类型
开发语言·后端·rust
阿正的梦工坊3 小时前
【Rust】08-集合类型、字符串与迭代器入门
开发语言·rust·c#
阿正的梦工坊6 小时前
【Rust】09-泛型、Trait 与生命周期基础
开发语言·rust·c#
阿正的梦工坊7 小时前
【Rust】07-错误处理:Option、Result 与 ? 运算符
开发语言·算法·rust
Jurio.8 小时前
开源 Codex Sticky:在终端 Codex CLI 长对话中始终固定底部输入框
linux·rust·github·开源软件·codex·codex cli
阿正的梦工坊10 小时前
【Rust】04-借用、引用与切片
java·数据库·rust
福大大架构师每日一题10 小时前
2026年6月TIOBE编程语言排行榜,Go语言排名第13,Rust语言排名12。关于Rust已进入平台期的报道似乎为时过早。
开发语言·golang·rust
咸甜适中11 小时前
rust语言学习笔记Trait(十六)Error(错误)
笔记·学习·rust
guyoung11 小时前
BoxAgnts 工具系统(4)——Tool Trait 与并发上下文模型
rust·agent·ai编程
techdashen13 小时前
What is maintenance, anyway?
开发语言·后端·rust