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 in use and extern crate statements
  • async - return a Future instead of blocking the current thread
  • await - suspend execution until the result of a Future is ready
  • break - exit a loop immediately
  • const - define constant items or constant raw pointers
  • continue - continue to the next loop iteration
  • crate - link an external crate or a macro variable representing the crate in which the macro is defined
  • dyn - dynamic dispatch to a trait object
  • else - fallback for if and if let control flow constructs
  • enum - define an enumeration
  • extern - link an external crate, function, or variable
  • false - Boolean false literal
  • fn - define a function or the function pointer type
  • for - loop over items from an iterator, implement a trait, or specify a higher-ranked lifetime
  • if - branch based on the result of a conditional expression
  • impl - implement inherent or trait functionality
  • in - part of for loop syntax
  • let - bind a variable
  • loop - loop unconditionally
  • match - match a value to patterns
  • mod - define a module
  • move - make a closure take ownership of all its captures
  • mut - denote mutability in references, raw pointers, or pattern bindings
  • pub - denote public visibility in struct fields, impl blocks, or modules
  • ref - bind by reference
  • return - return from function
  • Self - a type alias for the type we are defining or implementing
  • self - method subject or current module
  • static - global variable or lifetime lasting the entire program execution
  • struct - define a structure
  • super - parent module of the current module
  • trait - define a trait
  • true - Boolean true literal
  • type - define a type alias or associated type
  • union - define a union and is only a keyword when used in a union declaration
  • unsafe - denote unsafe code, functions, traits, or implementations
  • use - bring symbols into scope
  • where - denote clauses that constrain a type
  • while - 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相关推荐

  1. RUST直接升钢指令_[译]参照TypeScript学习Rust-part-1

    [译]参照TypeScript学习Rust-1 · 前端在线​regx.vip 对于前端,笔者比较认可Rust作为前端开发技术栈投资的,本文系列翻译旨在分享.学习Rust这门语言. Rust常常被认为 ...

  2. Rust 用于 STM32 开发

    我的开发板有两个了 LED 灯,做一个用按键控制两个灯的小测试.电路图如下: 可见 LED 连接在 PF9 和 PF10 引脚,按键连接在 PA0 引脚. 用 Cargo 创建一个 Rust 项目,并 ...

  3. Rust Cargo toml描述文件配置详细参考

    Cargo的toml描述文件配置字段详细参考 [package] 节点 [package] # 软件包名称,如果需要在别的地方引用,请使用它. name = "hello_world&quo ...

  4. rust绿卡名称_Rust学习笔记5 属性与Cargo配置

    5.1 属性 属性(Attribute)是一种通用的用于表达元数据的特性. 在rust中大量使用属性,对于go程序员而言,属性的大量使用很容易造成困惑. 属性有些类似与spring boot中的注解, ...

  5. Rust和C / C ++的跨语言链接时间优化LTO

    Rust和C / C ++的跨语言链接时间优化LTO 链接时间优化(LTO)是LLVM实施整个程序优化的方法.跨语言LTO是Rust编译器中的一项新功能,使LLVM的链接时间优化可以在混合的C / C ...

  6. 10玩rust_有趣的 Rust 类型系统: Trait

    也许你已经学习了标准库提供的 String 类型,这是一个 UTF-8 编码的可增长字符串.该类型的结构为: pub struct String {vec: Vec<u8>, } UTF- ...

  7. rust熔炉怎么带走_Rust游戏中12个实用小技巧,包含无伤下坠、直梯爬楼

    Rust是一款第一人称生存网络游戏,有点像野外求生,但这款游戏内容则更加丰富.刺激.血腥. 在这款游戏中玩家的第一任务就是活下来,而想要活下来你将要接受饥饿.干渴.寒冷等.游戏中玩家需要建造自己的庇护 ...

  8. 腐蚀rust电脑分辨率调多少_腐蚀Rust怎么设置画面 腐蚀Rust提高帧数画面设置方法...

    腐蚀Rust这个游戏的细节取决于图像质量也就是我们进入游戏的时候可以选择画质,这里为大家带来腐蚀Rust画质设置教程. 图像质量 1~3为一个大档 4~5是一个大档 4以上你在游戏里面的画面会显示更多 ...

  9. rust 官服指令_RUST 命令大全(包括服务器指令)

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 RUST++ MOD (以下在聊天框内输入) 基本命令 /share playername [shares your doors with a playe ...

最新文章

  1. QTP中对数据库的操作(查询,更新和删除等)
  2. 这三类问题是工控系统最大的威胁
  3. zcmu-2153(拓扑排序+优先队列)
  4. 安装Windows Nano Server虚拟机
  5. 论文小综 | Neuro-Symbolic Reasoning in NLP
  6. 每天一个linux 命令 find命令
  7. unity常连接mysql,Unity使用BestHttp插件时Socket.IO保持长连接的问题
  8. 使用反射创建实例/对象的两种方法
  9. java基础练习(持续更新)
  10. 空间变换与计算_02_3x3矩阵
  11. python控制雷电模拟器
  12. onenote打开闪退平板_win10系统下OneNote打不开或闪退怎么解决
  13. B2C大点名:国内B2C网站收集
  14. CH340g的使用说明
  15. EtherCAT运动控制卡的电子凸轮追剪飞剪等应用(一)
  16. 2020双十一AutoJs自动领喵币再次来袭【天猫、淘宝、支付宝】
  17. IP地址子网掩码的计算
  18. 基于MATLAB的人民币识别系统
  19. 背景渐变色,兼容IE
  20. 可视化例子(14)——ECharts波浪图(或者称为河流图)

热门文章

  1. Android 编译打包的那些疑问
  2. ClickHouse使用实践与规范
  3. flutter 真机iOS字体偏小
  4. pycharm运行后不显示run的结果
  5. IDEA远程连接虚拟机
  6. 视频播放网站CDN内容分发网络简单代码实现
  7. 机器学习入门(3)-——多元回归
  8. 越狱第三季第一集 在线免费观看
  9. 2021考研英语大作文写作必备15个话题
  10. Win11微信来消息任务栏右下角图标不闪烁怎么办?