Rust 函数

Rust 函数

简介

  • fn
1
2
3
4
// 函数声明
fn func_name(arg1: arg1_type) -> {
    // func_body
}

闭包(closure)

rust中closure 就是一个捕获了当前上下文变量的结构体

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
let m = 1.0;
let c = 2.0;

let line = |x| m*x + c;

// 等价于
struct SomeUnknownType<'a> {
    m: &'a f64,
    c: &'a f64
}
impl<'a> SomeUnknownType<'a> {
    fn call(&self, x: f64) -> f64 {
        self.m * x + self.c
    }
}

发散函数

高阶函数

参考

  1. [译]Rust返回引用的不同策略 | 鸟窝

  2. https://rust-lang.github.io/rfcs

  3. https://zhuanlan.zhihu.com/p/64417628

updatedupdated2024-05-102024-05-10