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 ...
随机推荐
- 编译原理中Follow集的求法
经过前阵子的各种百度以及对课本的反复研究,终于弄明白了follow集的求法,下面记录一下! 首先引用龙书里面的一段较为公式化的follow集求法的话: 计算所有非终结符号A的follow(A)集合时, ...
- Spark整合Hive
spark-sql 写代码方式 1.idea里面将代码编写好打包上传到集群中运行,上线使用 spark-submit提交 2.spark shell (repl) 里面使用sqlContext 测试使 ...
- Git知识总结
Git知识总结 Git安装 windows 在git官网中下载安装程序,然后按默认选项安装即可 安装完成后,在开始菜单里找到"Git"->"Git Bash&quo ...
- MySQL统计总数就用count(*),别花里胡哨的《死磕MySQL系列 十》
有一个问题是这样的统计数据总数用count(*).count(主键ID).count(字段).count(1)那个效率高. 先说结论,不用那么花里胡哨遇到统计总数全部使用count(*). 但是有很多 ...
- 菜鸡的Java笔记 - java 枚举
枚举 枚举属于加强版的多例设计模式 多例设计模式与枚举 多例设计模式的本质在于构造方法的私有化.而后在类的内部产生若干个实例化对象,随后利用一个 st ...
- Python 练习 人事管理
人事管理系统介绍:1.展示页面: ①首页: ==========欢迎来到简历管理系统v2.1.1========== 1.管理员登录 ...
- 菜鸡的Java笔记 第六 - java 方法
前提:现在所讲解的方法定义格式,只属于JAVA 方法定义的其中一种组成方式.而完整的组成方式将随着学习逐步渗透. 1.方法的基本定义 方法(Method)在一些书中也会有人将其说是 函数(Funct ...
- [hdu7032]Command and Conquer: Red Alert 2
令$(x,y,z)$为狙击手的坐标,其攻击范围即以$(x,y,z)$为中心的$(2k)^{3}$的立方体 为了避免$k$的影响(二分答案会多一个$\log$),不妨将其变为以$(x,y,z)$为左下 ...
- Java设计模式之(七)——装饰器模式
1.什么是装饰器模式? Attach additional responsibilities to an object dynamically keeping the same interface.D ...
- Docker极简入门:使用Docker运行Java程序
运行简单的Java程序 先在当前目录创建App.java文件 public class App{ public static void main(String[] args){ String os = ...