Rust 的一元是什么 ||(平行管)是什么意思? [英] What does Rust's unary || (parallel pipe) mean?

查看:32
本文介绍了Rust 的一元是什么 ||(平行管)是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非词法生命周期:介绍,Niko 包含以下代码段:

In Non-Lexical Lifetimes: Introduction, Niko includes the following snippet:

fn get_default3<'m,K,V:Default>(map: &'m mut HashMap<K,V>,
                                key: K)
                                -> &'m mut V {
    map.entry(key)
       .or_insert_with(|| V::default())
}

|| 是做什么的V::default() 在这里是什么意思?

推荐答案

这是一个零参数的闭包.这是一个显示基本语法和用法的简化示例(play):

It is a closure with zero arguments. This is a simplified example to show the basic syntax and usage (play):

fn main() {
    let c = || println!("c called");
    c();
    c();
}

打印:

c called
c called

另一个文档中的示例:

let plus_one = |x: i32| x + 1;

assert_eq!(2, plus_one(1));

这篇关于Rust 的一元是什么 ||(平行管)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆