gpt4 book ai didi

closures - &: part in a closure argument list? 是什么

转载 作者:行者123 更新时间:2023-11-29 08:08:06 27 4
gpt4 key购买 nike

看代码:

let add_one = |&: x| { 1 + x };

我知道x是闭包参数,但是闭包中的&:是什么意思?

最佳答案

这是 Rust 的一个文档不足的部分(并且过时,请参阅评论)。我知道最好的引用是博客文章 Purging proc :

Because the current inference scheme is limited, you will sometimes need to specify which of the three fn traits you want explicitly. (Some people also just prefer to do that.) The current syntax is to use a leading &:, &mut:, or :, kind of like an “anonymous parameter”:

// Explicitly create a `Fn` closure.
foo(|&:| { ... })

// Explicitly create a `FnMut` closure.
foo(|&mut:| { ... })

// Explicitly create a `FnOnce` closure.
foo(|:| { ... }) // (ERROR)

Caveat: It is still possible we’ll change the &:/&mut:/: syntax before 1.0; if we can improve inference enough, we might even get rid of it altogether.

看起来它在 #21843 中被删除了!感谢您指出这一点,@swizard!

关于closures - &: part in a closure argument list? 是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28395722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com