Rust: Keywords
The following list contains keywords that are reserved for current or future use by the Rust language. As such, they cannot be used as identifiers (except as raw identifiers as we’ll discuss in the “Raw Identifiers” section), including names of functions, variables, parameters, struct fields, modules, crates, constants, macros, static values, attributes, types, traits, or lifetimes.
Keywords Currently in Use
The following keywords currently have the functionality described.
as
- perform primitive casting, disambiguate the specific trait containing an item, or rename items inuse
andextern crate
statementsasync
- return aFuture
instead of blocking the current threadawait
- suspend execution until the result of aFuture
is readybreak
- exit a loop immediatelyconst
- define constant items or constant raw pointerscontinue
- continue to the next loop iterationcrate
- link an external crate or a macro variable representing the crate in which the macro is defineddyn
- dynamic dispatch to a trait objectelse
- fallback forif
andif let
control flow constructsenum
- define an enumerationextern
- link an external crate, function, or variablefalse
- Boolean false literalfn
- define a function or the function pointer typefor
- loop over items from an iterator, implement a trait, or specify a higher-ranked lifetimeif
- branch based on the result of a conditional expressionimpl
- implement inherent or trait functionalityin
- part offor
loop syntaxlet
- bind a variableloop
- loop unconditionallymatch
- match a value to patternsmod
- define a modulemove
- make a closure take ownership of all its capturesmut
- denote mutability in references, raw pointers, or pattern bindingspub
- denote public visibility in struct fields,impl
blocks, or modulesref
- bind by referencereturn
- return from functionSelf
- a type alias for the type we are defining or implementingself
- method subject or current modulestatic
- global variable or lifetime lasting the entire program executionstruct
- define a structuresuper
- parent module of the current moduletrait
- define a traittrue
- Boolean true literaltype
- define a type alias or associated typeunion
- define a union and is only a keyword when used in a union declarationunsafe
- denote unsafe code, functions, traits, or implementationsuse
- bring symbols into scopewhere
- denote clauses that constrain a typewhile
- loop conditionally based on the result of an expression
Keywords Reserved for Future Use
The following keywords do not have any functionality but are reserved by Rust for potential future use.
abstract
become
box
do
final
macro
override
priv
try
typeof
unsized
virtual
yield
Raw Identifiers
Raw identifiers are the syntax that lets you use keywords where they wouldn’t normally be allowed. You use a raw identifier by prefixing a keyword with r#
.
For example, match
is a keyword. If you try to compile the following function that uses match
as its name:
Filename: src/main.rs
fn match(needle: &str, haystack: &str) -> bool { haystack.contains(needle) }
you’ll get this error:
error: expected identifier, found keyword `match` --> src/main.rs:4:4 | 4 | fn match(needle: &str, haystack: &str) -> bool { | ^^^^^ expected identifier, found keyword
The error shows that you can’t use the keyword match
as the function identifier. To use match
as a function name, you need to use the raw identifier syntax, like this:
Filename: src/main.rs
fn r#match(needle: &str, haystack: &str) -> bool { haystack.contains(needle) } fn main() { assert!(r#match("foo", "foobar")); }
This code will compile without any errors. Note the r#
prefix on the function name in its definition as well as where the function is called in main
.
Raw identifiers allow you to use any word you choose as an identifier, even if that word happens to be a reserved keyword. In addition, raw identifiers allow you to use libraries written in a different Rust edition than your crate uses. For example, try
isn’t a keyword in the 2015 edition but is in the 2018 edition. If you depend on a library that’s written using the 2015 edition and has a try
function, you’ll need to use the raw identifier syntax, r#try
in this case, to call that function from your 2018 edition code. See Appendix E for more information on editions.
Rust: Keywords相关推荐
- RUST直接升钢指令_[译]参照TypeScript学习Rust-part-1
[译]参照TypeScript学习Rust-1 · 前端在线regx.vip 对于前端,笔者比较认可Rust作为前端开发技术栈投资的,本文系列翻译旨在分享.学习Rust这门语言. Rust常常被认为 ...
- Rust 用于 STM32 开发
我的开发板有两个了 LED 灯,做一个用按键控制两个灯的小测试.电路图如下: 可见 LED 连接在 PF9 和 PF10 引脚,按键连接在 PA0 引脚. 用 Cargo 创建一个 Rust 项目,并 ...
- Rust Cargo toml描述文件配置详细参考
Cargo的toml描述文件配置字段详细参考 [package] 节点 [package] # 软件包名称,如果需要在别的地方引用,请使用它. name = "hello_world&quo ...
- rust绿卡名称_Rust学习笔记5 属性与Cargo配置
5.1 属性 属性(Attribute)是一种通用的用于表达元数据的特性. 在rust中大量使用属性,对于go程序员而言,属性的大量使用很容易造成困惑. 属性有些类似与spring boot中的注解, ...
- Rust和C / C ++的跨语言链接时间优化LTO
Rust和C / C ++的跨语言链接时间优化LTO 链接时间优化(LTO)是LLVM实施整个程序优化的方法.跨语言LTO是Rust编译器中的一项新功能,使LLVM的链接时间优化可以在混合的C / C ...
- 10玩rust_有趣的 Rust 类型系统: Trait
也许你已经学习了标准库提供的 String 类型,这是一个 UTF-8 编码的可增长字符串.该类型的结构为: pub struct String {vec: Vec<u8>, } UTF- ...
- rust熔炉怎么带走_Rust游戏中12个实用小技巧,包含无伤下坠、直梯爬楼
Rust是一款第一人称生存网络游戏,有点像野外求生,但这款游戏内容则更加丰富.刺激.血腥. 在这款游戏中玩家的第一任务就是活下来,而想要活下来你将要接受饥饿.干渴.寒冷等.游戏中玩家需要建造自己的庇护 ...
- 腐蚀rust电脑分辨率调多少_腐蚀Rust怎么设置画面 腐蚀Rust提高帧数画面设置方法...
腐蚀Rust这个游戏的细节取决于图像质量也就是我们进入游戏的时候可以选择画质,这里为大家带来腐蚀Rust画质设置教程. 图像质量 1~3为一个大档 4~5是一个大档 4以上你在游戏里面的画面会显示更多 ...
- rust 官服指令_RUST 命令大全(包括服务器指令)
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 RUST++ MOD (以下在聊天框内输入) 基本命令 /share playername [shares your doors with a playe ...
最新文章
- QTP中对数据库的操作(查询,更新和删除等)
- 这三类问题是工控系统最大的威胁
- zcmu-2153(拓扑排序+优先队列)
- 安装Windows Nano Server虚拟机
- 论文小综 | Neuro-Symbolic Reasoning in NLP
- 每天一个linux 命令 find命令
- unity常连接mysql,Unity使用BestHttp插件时Socket.IO保持长连接的问题
- 使用反射创建实例/对象的两种方法
- java基础练习(持续更新)
- 空间变换与计算_02_3x3矩阵
- python控制雷电模拟器
- onenote打开闪退平板_win10系统下OneNote打不开或闪退怎么解决
- B2C大点名:国内B2C网站收集
- CH340g的使用说明
- EtherCAT运动控制卡的电子凸轮追剪飞剪等应用(一)
- 2020双十一AutoJs自动领喵币再次来袭【天猫、淘宝、支付宝】
- IP地址子网掩码的计算
- 基于MATLAB的人民币识别系统
- 背景渐变色,兼容IE
- 可视化例子(14)——ECharts波浪图(或者称为河流图)