Rust 函数
定义、返回值、参数、以及 `fn` 与闭包的分工。
定义与调用
fn greet(name: &str) -> String {
format!("你好,{}", name)
}
fn main() {
let msg = greet("Rust");
println!("{}", msg);
}你好,RustParameters must be typed and returns use ->. Rust does not infer types across function boundaries — deliberately, so the signature is a complete contract.
返回值
fn double(x: i32) -> i32 {
x * 2 // 表达式,不加分号
}
fn double_explicit(x: i32) -> i32 {
return x * 2; // 也可以显式 return
}
fn nothing() {
println!("没有返回值,实际返回 ()");
}
fn main() {
println!("{} {}", double(3), double_explicit(4));
nothing();
}6 8
没有返回值,实际返回 ()return is for early exits. A trailing return x; works but is unidiomatic and clippy flags it as needless_return.
多返回值用元组
fn divmod(a: i32, b: i32) -> (i32, i32) {
(a / b, a % b)
}
fn main() {
let (q, r) = divmod(17, 5);
println!("商 {} 余 {}", q, r);
// 也可以直接取
let t = divmod(10, 3);
println!("{} {}", t.0, t.1);
}商 3 余 2
3 1函数是一等公民
fn add(a: i32, b: i32) -> i32 { a + b }
fn sub(a: i32, b: i32) -> i32 { a - b }
fn apply(f: fn(i32, i32) -> i32, a: i32, b: i32) -> i32 {
f(a, b)
}
fn main() {
println!("{}", apply(add, 5, 3));
println!("{}", apply(sub, 5, 3));
// 存进 Vec
let ops: Vec<fn(i32, i32) -> i32> = vec![add, sub];
for op in ops {
println!("{}", op(10, 4));
}
}8
2
14
6fn(i32, i32) -> i32 is a function pointer type — plain functions only. Closures that capture need a generic F: Fn(i32, i32) -> i32.
嵌套与作用域
fn main() {
fn helper(x: i32) -> i32 {
x + 1
}
println!("{}", helper(1));
}2Functions may be nested but cannot capture outer variables — that is a closure's job. Nested functions mainly live in test modules or to keep a helper local.
impl Trait in return position is common: fn make() -> impl Iterator<Item = i32>. It hides the verbose type but allows only one concrete type.定义与调用
fn greet(name: &str) -> String {
format!("你好,{}", name)
}
fn main() {
let msg = greet("Rust");
println!("{}", msg);
}你好,Rust参数必须标类型,返回值用 ->。Rust 不做类型推断跨函数边界——这是刻意的,让函数签名成为完整的契约。
返回值
fn double(x: i32) -> i32 {
x * 2 // 表达式,不加分号
}
fn double_explicit(x: i32) -> i32 {
return x * 2; // 也可以显式 return
}
fn nothing() {
println!("没有返回值,实际返回 ()");
}
fn main() {
println!("{} {}", double(3), double_explicit(4));
nothing();
}6 8
没有返回值,实际返回 ()return 只用于提前返回。函数末尾用 return x; 能跑但不符合习惯,clippy 会提示 needless_return。
多返回值用元组
fn divmod(a: i32, b: i32) -> (i32, i32) {
(a / b, a % b)
}
fn main() {
let (q, r) = divmod(17, 5);
println!("商 {} 余 {}", q, r);
// 也可以直接取
let t = divmod(10, 3);
println!("{} {}", t.0, t.1);
}商 3 余 2
3 1函数是一等公民
fn add(a: i32, b: i32) -> i32 { a + b }
fn sub(a: i32, b: i32) -> i32 { a - b }
fn apply(f: fn(i32, i32) -> i32, a: i32, b: i32) -> i32 {
f(a, b)
}
fn main() {
println!("{}", apply(add, 5, 3));
println!("{}", apply(sub, 5, 3));
// 存进 Vec
let ops: Vec<fn(i32, i32) -> i32> = vec![add, sub];
for op in ops {
println!("{}", op(10, 4));
}
}8
2
14
6fn(i32, i32) -> i32 是函数指针类型,只能指向不带捕获的普通函数。带捕获的闭包要用泛型 F: Fn(i32, i32) -> i32。
嵌套与作用域
fn main() {
fn helper(x: i32) -> i32 {
x + 1
}
println!("{}", helper(1));
}2函数可以定义在函数内部,但不能捕获外部变量(那是闭包的事)。嵌套函数主要用于测试模块或把只在本函数用的工具收进来。
impl Trait 是常用的返回类型写法:fn make() -> impl Iterator<Item = i32>。它避免写出冗长的泛型类型,但只能返回一种具体类型。