Rust 学习笔记:关于闭包的练习题
Rust 学习笔记:关于闭包的练习题
参考视频:
- https://www.bilibili.com/video/BV1u5NZe7Eyz
问题 1
下列哪项最能描述为什么 Rust 会推断闭包参数/返回值类型,但不会推断顶层函数的参数/返回值类型的理由?
A. 由于向后兼容性需要与旧版 Rust 兼容。
B. 顶层函数可以是库外部接口的一部分,而闭包不能直接暴露。
C. 由于停机问题(halting problem),从数学上讲,Rust 推断顶层函数类型是不可能的。
D. 任何可分配给变量的东西都可以推断类型,而顶层函数不能分配给变量。
答:B。
问题 2
Rust 允许在闭包的参数中进行模式匹配,包括使用下划线。代码如下:
let f = |_| ();
let s = String::from("Hello");
f(s);
A. f 读取 s 然后丢弃结果。
B. f 对 s 没有影响。
C. f 导致 s 立即被丢弃。
D. f 捕获 s 在其环境中。
答:C。
以下程序能否通过编译?若能,输出是?
fn main() {
let mut s = String::from("hello");
let mut add_suffix = || s.push_str(", world!");
println!("{}", s);
add_suffix();
}
答:不能通过编译。
以下程序能否通过编译?若能,输出是?
fn main() {
let mut s = String::from("Hello");
let mut add_suffix = |s: &mut String| s.push_str(" world");
println!("{}", s);
add_suffix(&mut s);
}
答:可以通过编译。输出 Hello。
考虑该 API,空白处填写哪个 Fn trait 最合适?
fn for_each_mut<T, F: ____(&mut T)> (v: &mut Vec<T>, mut f: F) {
for x in v.iter_mut() {
f(x);
}
}
答:FnMut。
因为这个闭包需要多次调用,并且改变数组 v 中的元素值。
考虑该 API,空白处填写哪个 Fn trait 最合适?
pub struct Analyzer<F> {
postprocess: F,
}
impl<F: ___(i32) -> i32> Analyzer<F> {
fn process(&self, n: i32) -> i32 { /* ... */ }
pub fn pipeline(&self, n: i32) -> i32 {
let n = self.process(n);
(self.postprocess)(n)
}
}
答:Fn。
因为这个闭包需要改变所有权。