Replies: 41 comments 81 replies
-
fn main() {
} fn exec<'a, F: FnMut(&'a str)>(mut f: F) { |
Beta Was this translation helpful? Give feedback.
-
exec的参数f已经获得闭包的所有权了,操作是合法的。 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
这里'a的生命周期为什么要添加呢? |
Beta Was this translation helpful? Give feedback.
-
为什么这样子写编译不通过,而
这样子写就会通过 |
Beta Was this translation helpful? Give feedback.
-
我现在写 Rust 有时候还是会恍惚,在函数签名参数上如果要加
这几种写法都是有的吗。。有什么规律吗。。经常搞不清楚 |
Beta Was this translation helpful? Give feedback.
-
fn fn_once<F>(func: F)
where
F: FnMut() -> () + Copy, // 改动在这里
{
func();
}
fn main() {
let mut x = vec![1, 2, 3];
fn_once(|| x.push(2));
} 我这个为什么不行 |
Beta Was this translation helpful? Give feedback.
-
这一章学完感觉很混乱 |
Beta Was this translation helpful? Give feedback.
-
对于这个代码
我尝试在new函数中直接定义闭包函数,报错如下:
报错:
想不明白同样的闭包函数,为什么通过参数传入就是正确的,在构造函数里面写就是类型不匹配 |
Beta Was this translation helpful? Give feedback.
-
前面说 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
fn main() { 书中的例子。 |
Beta Was this translation helpful? Give feedback.
-
let sum = |x, y| x + y; |
Beta Was this translation helpful? Give feedback.
-
fn main() {
} fn exec<'a, F: FnMut(&'a str)>(mut f: F) {
|
Beta Was this translation helpful? Give feedback.
-
fn factory(x:i32) -> impl Fn(i32) -> i32 {
} 就算签名一样吧闭包类型也是不同的。。。 |
Beta Was this translation helpful? Give feedback.
-
为什么这单代码可以正常运行呢?闭包拿走了x的所有权,通过原来的x还可以进行调用? fn fn_once<F>(func: F)
where
F: FnOnce(usize) -> bool,
{
println!("{}", func(3));
}
fn main() {
let x = vec![1, 2, 3];
fn_once(|z|{z == x.len()});
println!("{}",x[0]);
}
|
Beta Was this translation helpful? Give feedback.
-
这节课是没有练习题吗? |
Beta Was this translation helpful? Give feedback.
-
大概看了下,这章不太感兴趣,用到了再来看吧 |
Beta Was this translation helpful? Give feedback.
-
疑问:mut仅作用于编译器的语法分析中?
以上代码可以运行,结果正确输出“hello”。但在exec函数申明中,没有申明mut f: F。 |
Beta Was this translation helpful? Give feedback.
-
在js里闭包是如此的自然一度让我以为所有语言的闭包都是这样的... |
Beta Was this translation helpful? Give feedback.
-
闭包中的参数可以是泛型参数吗 |
Beta Was this translation helpful? Give feedback.
-
感觉这一章写得不太好,有点难以理解,没有说透闭包的本质。实际上闭包可以理解成一个结构体,和 C++ 的 lambda 函数一样,都是用结构体来实现的。闭包捕获变量也就是会在结构体里面添加一个成员变量,这个成员变量根据捕获方式的不同可以是 值、不可变引用和可变引用。 |
Beta Was this translation helpful? Give feedback.
-
这里有问题吧,i32实现了copy但是这个闭包没有自动实现copy |
Beta Was this translation helpful? Give feedback.
-
factory的参数x 和 闭包的x 不是一个x,这样写有点误导了,可以把闭包的改成y |
Beta Was this translation helpful? Give feedback.
-
有无朋友解释一下为啥会有“闭包”这种打破封装的功能?换句话说为啥要让闭包可以捕获和操作外部变量呀? |
Beta Was this translation helpful? Give feedback.
-
讲的不清不楚的,Fn(i32) -> i32 这个没解释清楚,这里的 Fn 是一个 trait,但是写法就是语法糖,试了一下,解糖后不给调用,说不稳定。 // 这里说 Tuple 不稳定
pub trait MyFn<Args: std::marker::Tuple> {
type Output;
}
fn get_add_fun(x: i32) -> Box<dyn MyFn<(i32), Output=i32>> {
Box::new(move |y| x + y)
} 这样写在 RustRover get_add_fun 没有提示错误。 如果把 MyFn trait 改成下面这样不使用 Tuple: pub trait MyFn<Args> {
type Output;
} 编译后会说 MyFn 没有使用 closure。
把 get_add_fun 用解糖后的 Fn 去写 //fn get_add_fun(x: i32) -> Box<dyn Fn(i32) -> i32> {
// Box::new(move |y| x + y)
//}
fn get_add_fun(x: i32) -> Box<dyn Fn<(i32), Output=i32>> {
Box::new(move |y| x + y)
} 编译后直接告诉你,不能这么写。
如果解糖去调用,编译失败,然后给了一个 issue 地址。 fn main() {
let add_one = get_add_fun(1);
println!("1 + 2 = {}", add_one.call(2));
}
|
Beta Was this translation helpful? Give feedback.
-
记录一下对于 判断closure实现了哪种
关于closure在使用时的约束:
|
Beta Was this translation helpful? Give feedback.
-
这章很有意思,收获颇丰 |
Beta Was this translation helpful? Give feedback.
-
所有权转移到哪去了呢?🤔 |
Beta Was this translation helpful? Give feedback.
-
https://course.rs/advance/functional-programing/closure.html
Beta Was this translation helpful? Give feedback.
All reactions