4.3 rust func closure
fn add_one_v1 (x: u32) -> u32 { x + 1 }
let add_one_v2 = |x: u32| -> u32 { x + 1 };
let add_one_v3 = |x| { x + 1 };
let add_one_v4 = |x| x + 1 ;
pub fn test3() {
let example_closure = |x| x;
let s = example_closure(String::from("hello"));
let n = example_closure(5);
}
--> src/base/k_closure.rs:53:29
|
53 | let n = example_closure(5);
| ^
| |
| expected struct `String`, found integer
| help: try using a conversion method: `5.to_string()`
The first time we call example_closure with the String value, the compiler infers the type of x and the return type of the closure to be String. Those types are then locked into the closure in example_closure, and we get a type error if we try to use a different type with the same closure.
不换数据类型,下面的是正确的
pub fn test3() {
let example_closure = |x| x;
let s = example_closure(6);
println!("{}",s);
let n = example_closure(5);
println!("{}",n);
}
6
5
4.3 rust func closure的更多相关文章
- Complexity Behind Closure
这篇文章同时发布在github上 这篇文章是我对ooc编译器里一个小bug调试时作的手记.虽然相信大多数人对编译器(并且是一门小众语言的编译器)并不感兴趣,但这篇文章可以给C用户们提供一些Object ...
- PHP Closure创建匿名函数
Closure 类 用于代表匿名函数的类. 匿名函数(在 PHP 5.3 中被引入)会产生这个类型的对象.在过去,这个类被认为是一个实现细节,但现在可以依赖它做一些事情.自 PHP 5.4 起, 这个 ...
- golang基础--func函数
函数function Go函数不支持 嵌套, 重载和默认参数 支持以下特性: 无须声明原型,不定长度长度变参,多返回值,命名返回值参数,匿名函数,闭包 定义函数使用关键字func,且左侧大括号不能另起 ...
- php Closure::bind的参数说明
publicstatic Closure Closure::bind ( Closure $closure , object$newthis [, mixed$newscope = 'static' ...
- swift语言特性
最近苹果推出了他们新的开发语言,swift,他们自己的说法是,swift语言将会更快捷,更安全等等.但是具体的性能,还需要在后面的实践过程中去观察,但是就目前来说swift语言除了将大部分21世纪静态 ...
- Block 实践
OC版 函数中无参无返回值 /* 作为函数参数类型的格式 返回值类型 (^)(形参列表) */ CZPerson.h - (void) test:(void (^)(void))block; CZPe ...
- GO入门——5. 函数
1 函数 Go 函数 不支持 嵌套.重载和默认参数 定义函数使用关键字 func,且左大括号不能另起一行 函数也可以作为一种类型使用 无需声明原型 不定长度变参 func A(a string,c . ...
- Golang 函数function
函数function Go函数不支持嵌套.重载和默认参数 但支持以下特性: 无需声明原型 不定长度变参 多返回值 命名返回值参数 匿名函数 闭包 定义函数使用关键字func,且左大括号不能另起一行 函 ...
- GO_05:GO语言基础map与函数
1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...
随机推荐
- CentOS7 导入oracle数据
1.切换到 oracle用户 #su - oracle 2.导入(一般的不会导入到sys账号下) #imp sys/密码@orcl file=/home/oracle/20200428.dmp fro ...
- 记一次排查CPU高的问题
背景 将log4j.xml的日志级别从error调整为info后,进行压测发现CPU占用很高达到了90%多(之前也就是50%,60%的样子). 问题排查 排查思路: 看进程中的线程到底执行的是什么, ...
- 端口扫描工具 nmap 使用手册
0x00 主机发现 -sL 仅仅是显示,扫描的IP数目,不会进行任何扫描 -sn ping扫描,即主机发现 -Pn 不检测主机存活 -PS/PA/PU/PY[portlist] TCP SYN Pin ...
- Mysql基础教程:(七)MySQL基础练习
MySQL基础练习 一.创建student和score表 CREATE TABLE student (id INT(10) NOT NULL PRIMARY KEY ,name VARCHAR(20) ...
- Ubuntu系统下《汇编语言》环境配置
说明 1.系统:Ubuntu codists@pc:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Des ...
- A Modified Particle Swarm Optimizer
A Modified Particle Swarm Optimizer 一种改进的粒子群优化算法 Yuhui Shi and Russell Eberhart 1998 摘要: 本文在原有的粒 ...
- [loj3525]喷泉公园
先将整张图$x$和$y$都缩小一半,即"道路"长度变为1,"长椅"变为放在格子中心 如果在没有长椅的限制下也无解(直接dfs即可判定),显然原问题也无解 否则 ...
- [loj3501]图函数
$f(i,G)_{x}$为$x$对$i$是否有贡献,即在枚举到$x$时,$i$与$x$是否强连通 事实上,$f(i,G)_{x}=1$即不经过$[1,x)$中的点且$i$与$x$强连通 首先,当存在这 ...
- [atARC094F]Normalization
考虑$s$能变成$t$的必要条件(假设$s\ne t$): 1.$s$中存在一对相邻字符不同 2.$|s|=|t|$且若将a-c对应为0-2,则字符模3同余: 3.$t$中存在一对相邻两个字符相同 同 ...
- 类的访问权限和Object
1.访问控制权限 1.1.访问控制权限都有哪些? 4个. private 私有 public 公开 protected 受保护 默认 1.2.以上的4个访问控制权限:控制的范围是什么? private ...