当前位置: 首页 > news >正文

Rust- 闭包

A closure in Rust is an anonymous function you can save in a variable or pass as an argument to another function. You can create the closure using a lightweight syntax and access variables from the scope in which it’s defined.

Here’s an example of a closure that increases a number by one:

let plus_one = |x: i32| x + 1;let a = 5;
println!("{}", plus_one(a)); // Outputs 6

In this example, plus_one is a closure that takes one argument x and returns x + 1.

Closures in Rust are similar to lambdas in other languages, but they have some specific behaviors and capabilities.

  • Capture Environment: Closures have the ability to capture values from the scope in which they’re defined. Here’s an example:

    let num = 5;
    let plus_num = |x: i32| x + num;
    println!("{}", plus_num(10)); // Outputs 15
    

    Here, plus_num captures the value of num from the surrounding environment.

  • Flexible Input Types: Unlike functions, closures don’t require you to annotate the types of the input parameters. However, you can if you want to, and sometimes doing so can increase clarity.

  • Three Flavors of Closures: Closures in Rust come in three forms, which differ in how they capture variables from the surrounding scope: Fn, FnMut, and FnOnce. The type is chosen by the compiler based on how the closure uses variables from the environment.

    • FnOnce consumes the variables it captures from its enclosing scope, known as the “once” because it can’t take ownership more than once.
    • FnMut can change the environment because it mutably borrows values.
    • Fn borrows values from the environment immutably.

Let’s start with an overview of each:

  1. FnOnce captures variables and moves them into the closure when it is defined. It can consume those variables when the closure is called. FnOnce closures can’t be called more than once in some contexts without duplication.

  2. FnMut can mutate and also consume variables from the environment in which it is defined.

  3. Fn borrows variables from the environment immutably.

Now, let’s have a look at some examples for each type:

// Example of FnOnce
let x = "hello".to_string();
let consume_x = move || println!("{}", x);
consume_x(); // "hello"
// consume_x(); This won't compile because `x` has been moved into the closure// Example of FnMut
let mut y = "hello".to_string();
let mut append_world = || y.push_str(" world");
append_world();
println!("{}", y); // "hello world"// Example of Fn
let z = "hello".to_string();
let print_z = || println!("{}", z);
print_z();
print_z(); // We can call this as many times as we want.

① In the first example, the closure takes ownership of x (indicated by the move keyword), so it’s a FnOnce.

② In the second example, the closure mutably borrows y, so it’s a FnMut.

③ In the third example, the closure immutably borrows z, so it’s a Fn.

Rust chooses how to capture variables on the fly without any annotation required. The move keyword is used to force the closure to take ownership of the values it uses.

Closures are a fundamental feature for many Rust idioms. They are used extensively in iterator adapters, threading, and many other situations.

fn main() {/*|| 代替 ()将输入参数括起来函数体界定符是{},对于单个表达式是可选的,其他情况必须加上。有能力捕获外部环境的变量。|参数列表| {业务逻辑}|| {业务逻辑}闭包可以赋值一个变量。*/let double = |x| {x * 2};let add = |a, b| {a + b};let x = add(2, 4);println!("{}", x);          // 6let y = double(5);println!("{}", y);          // 10let v = 3;let add2 = |x| {v + x};println!("{}", add2(4));    // 7/*闭包,可以在没有标注的情况下运行。可移动(move), 可借用(borrow), 闭包可以通过引用 &T可变引用 &mut T值 T捕获1. 闭包是一个在函数内创建立即调用的另外一个函数。2. 闭包是一个匿名函数3. 闭包虽然没有函数名,但可以把整个闭包赋值一个变量,通过该变量来完成闭包的调用4. 闭包不用声明返回值,但可以有返回值。并且使用最后一条语句的执行结果作为返回值,闭包的返回值也可以给变量。5. 闭包也称之为内联函数,可以访问外层函数里的变量。*/
}
fn main() {let add = |x, y| x + y;let result = add(3, 4);println!("{}", result); // 7receives_closure(add); // // 闭包作为参数执行结果 => 8let y = 2;receives_closure2(|x| x + y); // closure(1) => 3let y = 3;receives_closure2(|x| x + y); // closure(1) => 4let closure = returns_closure();println!("返回闭包 => {}", closure(1)); // 返回闭包 => 7let result = do1(add, 5);println!("result(1) => {}", result(1)); // result(1) => 6let result = do2(add, 5);println!("result(2) => {}", result(2)); // result(2) => 7
}fn do2<F, X, Y, Z>(f: F, x: X) -> impl Fn(Y) -> Z
whereF: Fn(X, Y) -> Z,X: Copy,
{move |y| f(x, y)
}// 参数和返回值都有闭包
fn do1<F>(f: F, x: i32) -> impl Fn(i32) -> i32
whereF: Fn(i32, i32) -> i32,
{move |y| f(x, y)
}// 返回闭包
fn returns_closure() -> impl Fn(i32) -> i32 {|x| x + 6
}// 闭包作为参数
fn receives_closure<F>(closure: F)
whereF: Fn(i32, i32) -> i32,
{let result = closure(3, 5);println!("闭包作为参数执行结果 => {}", result)
}// 闭包捕获变量
fn receives_closure2<F>(closure: F)
whereF: Fn(i32) -> i32,
{let result = closure(1);println!("closure(1) => {}", result);
}

相关文章:

Rust- 闭包

A closure in Rust is an anonymous function you can save in a variable or pass as an argument to another function. You can create the closure using a lightweight syntax and access variables from the scope in which it’s defined. Here’s an example of a clo…...

【数据挖掘torch】 基于LSTM电力系统负荷预测分析(Python代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…...

「JVM」性能调优工具

「JVM」性能调优工具 一、jcmd1、jcmd 能干嘛&#xff1f;2、与JVM相关的命令3、示例 二、jmap1、jmap有什么用&#xff1f;2、jmap的命令大全3、示例 三、jps1、jps有什么用&#xff1f;2、jps命令以及示例 四、jstat1、jstat有什么用&#xff1f;2、jstat命令以及示例 五、js…...

IDEA Debug小技巧 添加减少所查看变量、查看不同线程

问题 IDEA的Debug肯定都用过。它下面显示的变量&#xff0c;有什么门道&#xff1f;可以增加变量、查看线程吗&#xff1f; 答案是&#xff1a;可以。 演示代码 代码如下&#xff1a; package cn.itcast.attempt.threadAttempt.attempt2;public class Test {public static …...

基于SpringBoot+Vue的车辆充电桩管理系统设计与实现(源码+LW+部署文档等)

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…...

Bean的加载方式

目录 1. 基于XML配置文件 2. 基于XML注解方式声明bean 自定义bean 第三方bean 3.注解方式声明配置类 扩展1&#xff0c;FactoryBean 扩展2,加载配置类并加载配置文件&#xff08;系统迁移) 扩展3&#xff0c;proxyBeanMethodstrue的使用 4. 使用Import注解导入要注入的bean…...

《吐血整理》进阶系列教程-拿捏Fiddler抓包教程(13)-Fiddler请求和响应断点调试

1.简介 Fiddler有个强大的功能&#xff0c;可以修改发送到服务器的数据包&#xff0c;但是修改前需要拦截&#xff0c;即设置断点。设置断点后&#xff0c;开始拦截接下来所有网页&#xff0c;直到取消断点。这个功能可以在数据包发送之前&#xff0c;修改请求参数&#xff1b…...

Android 13(T) - Media框架(1)- 总览

从事Android Media开发工作三年有余&#xff0c;刚从萌新变成菜鸟&#xff0c;一路上跌跌撞撞学习&#xff0c;看了很多零零碎碎的知识&#xff0c;为了加深对Android Media框架的理解&#xff0c;决定在这里记录下学习过程中想到的一些问题以及一些思考&#xff0c;也希望对初…...

简述vue3(ts)+antdesignvue项目框架搭建基本步骤

目录 项目简介 概念 过程简述 基本步骤 1.创建新项目 2.安装Ant Design Vue 3.配置Ant Design Vue 4.创建页面和组件 5.使用组件 6.运行项目 项目简介 概念 Vue 3&#xff08;使用TypeScript&#xff09;和Ant Design Vue项目框架搭建是指在Vue 3框架下&#xff0c;…...

webpack : 无法加载文件 C:\Program Files\nodejs\webpack.ps1

webpack : 无法加载文件 C:\Program Files\nodejs\webpack.ps1 1.问题2. 解决办法&#xff1a; 1.问题 使用webpack打包是报错如下&#xff1a; webpack : 无法加载文件 C:\Program Files\nodejs\webpack.ps1&#xff0c;因为在此系统上禁止运行脚本。有关详细信息&#xff0c…...

GDAL OGR C++ API 学习之路 (5)OGRLayer篇 代码示例

GetStyleTable virtual OGRStyleTable *GetStyleTable () 返回图层样式表 返回: 指向不应由调用方修改或释放的样式表的指针 // 假设图层对象为 poLayer OGRStyleTable* poStyleTable poLayer->GetStyleTable(); if (poStyleTable ! nullptr) {// 处理样式表信息// ..…...

NIDEC COMPONENTS尼得科科宝滑动型DIP开关各系列介绍

今天AMEYA360对尼得科科宝电子滑动型DIP开关各系列参数进行详细介绍&#xff0c;方便大家选择适合自己的型号。 系列一、滑动型DIP开关 CVS 针脚数&#xff1a;1, 2, 3, 4, 8 安装类型&#xff1a;表面贴装&#xff0c;通孔 可水洗&#xff1a;无 端子类型&#xff1a;PC引脚(只…...

一起学算法(滑动窗口篇)

前言&#xff1a; 对于滑动窗口&#xff0c;有长度固定的窗口&#xff0c;也有长度可变的窗口&#xff0c;一般是基于数组进行求解&#xff0c;对于一个数组中两个相邻的窗口&#xff0c;势必会有一大部分重叠&#xff0c;这部分重叠的内容是不需要重复计算的&#xff0c;所以我…...

HTML <q> 标签

实例 标记短的引用: <q>Here is a short quotation here is a short quotation</q>浏览器支持 元素ChromeIEFirefoxSafariOpera<q>YesYesYesYesYes所有浏览器都支持 <q> 标签。 定义和用法 <q> 标签定义短的引用。 浏览器经常在引用的内容…...

机器学习02-再识K邻近算法(自定义数据集训练及测试)

定义&#xff1a; 如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别&#xff0c;则该样本也属于这个类别。简单的说就是根据你的“邻居”来推断出你的类别。 用个成语就是物以类聚 思想&#xff1a; 如果一个样本在特征空间中的K个最…...

github使用笔记及git协作常用命令

1.Github有一个主库,每个人自己也有一个库,称为分支。 2.Github的协作流程:先从主库fork出自己的分支, 然后进行代码的修改等操作, 操作完之后从本地库上推到自己的服务器分支,然后 服务器分支Pull Request到 主库。 3.本地仓库由git维护的三棵“树"组成:第1个…...

iOS - Apple开发者账户添加新测试设备

获取UUID 首先将设备连接XCode&#xff0c;打开Window -> Devices and Simulators&#xff0c;通过下方位置查看 之后登录(苹果开发者网站)[https://developer.apple.com/account/] &#xff0c;点击设备 点击加号添加新设备 填写信息之后点击Continue&#xff0c;并一路继续…...

vue 前端 邮箱、密码、手机号码等输入验证规则

最近在写前端表单验证的时候&#xff0c;发现一篇文章质量很好&#xff0c;所以写下这篇文章记录 原文章链接&#xff1a;vue 邮箱、密码、手机号码等输入验证规则 1.手机号 const checkPhone (rule, value, callback) > {const phoneReg /^1[34578]\d{9}$$/;if (!value…...

如何看待前端已死这个问题(大学生篇)

小编刚大学毕业&#xff0c;还记得是大三的时候选择的前端开发方向&#xff0c;那个时候行情其实并没有这么差&#xff0c;最近互联网上讨论这一个很火的话题&#xff0c;叫前端已死。那么我就说说我的看法吧&#xff0c;虽然可能比起行业的大佬会比较短浅&#xff0c;但我想就…...

揭开高级产品经理思维的秘密

我经常被问到产品经理如何晋升到更高级别。事实上&#xff0c;获得晋升往往是一场复杂的游戏。是的&#xff0c;你的技能和成就很重要&#xff0c;但其他因素也很重要&#xff0c;比如你的经理对人才培养的关心程度、你的同事有多优秀、任期有多长、公司的政治氛围如何等等。 所…...

QMCDecode macOS音频解密终极指南:3分钟学会QQ音乐加密格式破解

QMCDecode macOS音频解密终极指南&#xff1a;3分钟学会QQ音乐加密格式破解 【免费下载链接】QMCDecode QQ音乐QMC格式转换为普通格式(qmcflac转flac&#xff0c;qmc0,qmc3转mp3, mflac,mflac0等转flac)&#xff0c;仅支持macOS&#xff0c;可自动识别到QQ音乐下载目录&#xf…...

BepInEx框架深度解析:游戏运行时注入的架构哲学与实践范式

BepInEx框架深度解析&#xff1a;游戏运行时注入的架构哲学与实践范式 【免费下载链接】BepInEx Unity / XNA game patcher and plugin framework 项目地址: https://gitcode.com/GitHub_Trending/be/BepInEx BepInEx不仅仅是一个Unity/XNA游戏的插件框架&#xff0c;它…...

超宽带天线设计原理与工程实践

1. 超宽带天线设计基础与核心挑战在无线通信技术快速发展的今天&#xff0c;超宽带(UWB)天线因其独特的工作机制和性能特点&#xff0c;正在雷达探测、精确定位和高速数据传输等领域展现出不可替代的价值。与传统窄带天线不同&#xff0c;UWB天线需要在极宽的频率范围内&#x…...

HTTPie CLI离线模式终极指南:10个调试和构建请求的秘诀

HTTPie CLI离线模式终极指南&#xff1a;10个调试和构建请求的秘诀 【免费下载链接】cli &#x1f967; HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more. 项目地址: https:…...

多模态AI技术助力听障沟通:HI-TransPA系统解析

1. 项目背景与核心价值作为一名长期关注无障碍技术发展的从业者&#xff0c;我见证了太多听障人士在语音沟通场景中面临的困境。传统的手语翻译服务存在人力成本高、响应延迟大等问题&#xff0c;而市面上大多数语音转文字工具又难以处理复杂的环境音和方言口音。这就是我们团队…...

Go语言HTTP客户端限流中间件goclaw实战:原理、配置与避坑指南

1. 项目概述与核心价值 最近在折腾一个需要处理大量网络爬虫任务的后台服务&#xff0c;团队里的小伙伴提到了一个叫 smallnest/goclaw 的开源项目。说实话&#xff0c;第一眼看到这个名字&#xff0c;我以为是某个新的爬虫框架或者代理工具。深入了解后才发现&#xff0c;它…...

GITA:面向视觉-语言图推理的图到视觉与文本集成

论文结构化摘要:GITA:面向视觉-语言图推理的图到视觉与文本集成 1. 核心研究问题 (PICO/T) P (问题/群体): 当前基于大型语言模型(LLM)的图推理方法仅使用文本描述来表示图结构,忽略了直观的视觉模态。现有的图神经网络(GNN)虽然擅长图推理,但泛化性、灵活性和用户友好…...

[具身智能-532]:Trae软件为例,哪些部分MCP host,哪部分是MCP Agent,哪部分是MCP Client,,哪部分是MCP Server,哪部分是MCP 大模型?

在 Trae 这款 AI 原生 IDE 中&#xff0c;这些概念并非抽象的理论&#xff0c;而是对应着具体的软件模块、配置文件和功能界面。结合 Trae 的架构设计&#xff08;基于 2026 年的最新版本&#xff09;&#xff0c;我为你拆解了每个部分在软件中的具体对应位置&#xff0c;并补充…...

绝区零自动化工具完整指南:解放双手的游戏助手终极配置教程

绝区零自动化工具完整指南&#xff1a;解放双手的游戏助手终极配置教程 【免费下载链接】ZenlessZoneZero-OneDragon 绝区零 一条龙 | 全自动 | 自动闪避 | 自动每日 | 自动空洞 | 支持手柄 项目地址: https://gitcode.com/gh_mirrors/ze/ZenlessZoneZero-OneDragon Zen…...

战略质量保障:从缺陷预测到全生命周期质量管理

1. 战略质量保障&#xff1a;破解软件开发中的质量困局在软件开发领域&#xff0c;我们常常面临一个令人头疼的悖论&#xff1a;所有人都认同质量的重要性&#xff0c;但真正投入资源进行质量保障时&#xff0c;却总是拖到项目后期&#xff0c;此时预算所剩无几&#xff0c;交付…...