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的更多相关文章

  1. Complexity Behind Closure

    这篇文章同时发布在github上 这篇文章是我对ooc编译器里一个小bug调试时作的手记.虽然相信大多数人对编译器(并且是一门小众语言的编译器)并不感兴趣,但这篇文章可以给C用户们提供一些Object ...

  2. PHP Closure创建匿名函数

    Closure 类 用于代表匿名函数的类. 匿名函数(在 PHP 5.3 中被引入)会产生这个类型的对象.在过去,这个类被认为是一个实现细节,但现在可以依赖它做一些事情.自 PHP 5.4 起, 这个 ...

  3. golang基础--func函数

    函数function Go函数不支持 嵌套, 重载和默认参数 支持以下特性: 无须声明原型,不定长度长度变参,多返回值,命名返回值参数,匿名函数,闭包 定义函数使用关键字func,且左侧大括号不能另起 ...

  4. php Closure::bind的参数说明

    publicstatic Closure Closure::bind ( Closure $closure , object$newthis [, mixed$newscope = 'static' ...

  5. swift语言特性

    最近苹果推出了他们新的开发语言,swift,他们自己的说法是,swift语言将会更快捷,更安全等等.但是具体的性能,还需要在后面的实践过程中去观察,但是就目前来说swift语言除了将大部分21世纪静态 ...

  6. Block 实践

    OC版 函数中无参无返回值 /* 作为函数参数类型的格式 返回值类型 (^)(形参列表) */ CZPerson.h - (void) test:(void (^)(void))block; CZPe ...

  7. GO入门——5. 函数

    1 函数 Go 函数 不支持 嵌套.重载和默认参数 定义函数使用关键字 func,且左大括号不能另起一行 函数也可以作为一种类型使用 无需声明原型 不定长度变参 func A(a string,c . ...

  8. Golang 函数function

    函数function Go函数不支持嵌套.重载和默认参数 但支持以下特性: 无需声明原型 不定长度变参 多返回值 命名返回值参数 匿名函数 闭包 定义函数使用关键字func,且左大括号不能另起一行 函 ...

  9. GO_05:GO语言基础map与函数

    1. map 1. 类似其它语言中的哈希表活着字典,以 key-value 形式存储数据 2. key 必须是支持 == 或 != 比较运算的类型,不可以是函数.map 或 slice 3. map ...

随机推荐

  1. redis 的单机安装

    redis 单机安装 参考文档地址:https://www.cnblogs.com/withfeel/p/10655994.html 1,下载redis,下载地址http://download.red ...

  2. 【Go语言学习笔记】hello world

    书接上回,上回说到了为什么要学习Go语言,今天我们来实际写一下,感受一下Go语言的精美之处. 环境搭建 安装和设置 Windows: Go安装包下载网址:https://golang.org/dl/ ...

  3. prometheus(6)之常用服务监控

    监控常用服务 1.tomcat 2.redis 3.mysql 4.nginx 5.mongodb prometheus监控tomcat tomcat_exporter地址 https://githu ...

  4. mysql 免密码登录

    mysql 8免密码登录 UPDATE mysql.user SET authentication_string=null WHERE User='root'; FLUSH PRIVILEGES; e ...

  5. Spring的轻量级实现

    作者: Grey 原文地址:Spring的轻量级实现 本文是参考公众号:码农翻身 的从零开始造Spring 教程的学习笔记 源码 github 开发方法 使用TDD的开发方法,TDD的开发流程是: 写 ...

  6. 12组-Alpha冲刺-1/6

    一.基本情况 队名:字节不跳动 组长博客:https://www.cnblogs.com/147258369k/p/15526363.html 小组人数:10人 二.冲刺概况汇报 侯钦凯 过去两天完成 ...

  7. 大数据学习——搭建第一台Hadoop主机

    类型:学习笔记 参考:尚硅谷大数据系列教程 工具准备 1.VMware 2.CentOS 7 最小安装版 3.远程工具推荐使用 FinalShell 安装系统 1.打开VMware,根据自己的情况配置 ...

  8. Java包装类,以及Integer与int之间的比较

    一.Java的基本类型 Java语言中提供了八种基本类型,包括六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型. 整数型,包括byte.short.int.long,默认初始值是0 ...

  9. 单元测试NUnit,mock组件NSubstitute,信号量SemaphoreSlim,异步lock等例子

    public class LockTest { private IDatabase _database; private readonly Random _random = new Random(); ...

  10. 如何用LOTO示波器TDR方法测试电线长度?

    TDR也就是时域反射(Time-domain reflectometer),它可以通过观察导线中反射回来的电信号波形对导线长度进行测量,或者对传输导线的阻抗特性进行分析评估. 我们经常会碰到的TDR的 ...