Rust关键字实例解析

打印 上一主题 下一主题

主题 1010|帖子 1010|积分 3030

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Rust是一种注重安全性、并发性和性能的系统编程语言。在Rust中,关键字是保留的标识符,用于语言的特定语法结构。这些关键字不能用作普通的标识符,除非使用原始标识符(raw identifiers)。下面,我们将通过实例详细先容Rust中当前使用的关键字及其功能。
当前使用的关键字及着实例

as

用于实行原始类型转换或消除trait中条目标歧义。
  1. let x: i32 = 5;
  2. let y: f64 = x as f64; // 将i32类型的x转换为f64类型
复制代码
async

返回一个Future而不是阻塞当前线程。
  1. async fn async_function() {
  2.     // 异步代码
  3. }
复制代码
await

挂起实行直到Future的结果准备好。
  1. async fn get_data() -> String {
  2.     async {
  3.         "Data".to_string()
  4.     }
  5.     .await
  6. }
复制代码
break

立即退出循环。
  1. for i in 0..10 {
  2.     if i == 5 {
  3.         break; // 当i等于5时退出循环
  4.     }
  5. }
复制代码
const

定义常量项。
  1. const MAX_POINTS: u32 = 100_000;
复制代码
continue

继续到下一个循环迭代。
  1. for i in 0..10 {
  2.     if i % 2 == 0 {
  3.         continue; // 跳过偶数,继续下一次循环
  4.     }
  5.     println!("{}", i);
  6. }
复制代码
crate

在模块路径中,指的是crate根。
  1. mod my_crate {
  2.     // 模块内容
  3. }
复制代码
dyn

动态派送到trait对象。
  1. trait Draw {
  2.     fn draw(&self);
  3. }
  4. struct Screen;
  5. impl Draw for Screen {
  6.     fn draw(&self) {
  7.         println!("Drawing screen");
  8.     }
  9. }
  10. let screen: &dyn Draw = &Screen;
  11. screen.draw();
复制代码
else

if和if let控制流构造的备选方案。
  1. let condition = true;
  2. if condition {
  3.     println!("Condition is true");
  4. } else {
  5.     println!("Condition is false");
  6. }
复制代码
enum

定义一个枚举。
  1. enum Message {
  2.     Quit,
  3.     Move { x: i32, y: i32 },
  4.     Write(String),
  5. }
复制代码
extern

链接一个外部函数或变量。
  1. extern crate rand;
  2. use rand::Rng;
  3. let mut rng = rand::thread_rng();
  4. let n: u32 = rng.gen();
复制代码
false

布尔假字面量。
  1. let is_active: bool = false;
复制代码
fn

定义一个函数。
  1. fn double(x: i32) -> i32 {
  2.     x * 2
  3. }
复制代码
for

循环遍历迭代器中的项。
  1. for item in vec![1, 2, 3] {
  2.     println!("{}", item);
  3. }
复制代码
if

根据条件表达式的结果进行分支。
  1. let condition = true;
  2. if condition {
  3.     println!("Condition is true");
  4. }
复制代码
impl

实现固有或trait功能。
  1. struct Rectangle {
  2.     width: u32,
  3.     height: u32,
  4. }
  5. impl Rectangle {
  6.     fn area(&self) -> u32 {
  7.         self.width * self.height
  8.     }
  9. }
复制代码
in

for循环语法的一部门。
  1. for i in 0..10 {
  2.     println!("{}", i);
  3. }
复制代码
let

绑定一个变量。
  1. let number = 5;
复制代码
loop

无条件循环。
  1. loop {
  2.     println!("Infinite loop");
  3.     break; // 需要显式break退出
  4. }
复制代码
match

将值匹配到模式。
  1. let num = Some(4);
  2. match num {
  3.     Some(x) => println!("Num is {}", x),
  4.     None => println!("No num"),
  5. }
复制代码
mod

定义一个模块。
  1. mod math_functions {
  2.     pub fn add(x: i32, y: i32) -> i32 {
  3.         x + y
  4.     }
  5. }
复制代码
move

使闭包取得其捕获的全部全部权。
  1. let text = "Hello".to_string();
  2. let move_text = move || {
  3.     println!("{}", text);
  4. };
  5. move_text();
复制代码
mut

在引用、原始指针或模式绑定中表现可变性。
  1. let mut num = 5;
  2. num += 1;
复制代码
pub

在结构体字段、impl块或模块中表现公共可见性。
  1. pub struct Point {
  2.     pub x: i32,
  3.     pub y: i32,
  4. }
复制代码
ref

通过引用绑定。
  1. let x = 5;
  2. let y: &i32 = &x;
复制代码
return

从函数返回。
  1. fn double(x: i32) -> i32 {
  2.     return x * 2;
  3. }
复制代码
Self

我们正在定义或实现的类型的类型别名。
  1. impl Rectangle {
  2.     fn area(&self) -> u32 {
  3.         self.width * self.height
  4.     }
  5. }
复制代码
self

方法主体或当前模块。
  1. impl Rectangle {
  2.     fn area(&self) -> u32 {
  3.         self.width * self.height
  4.     }
  5. }
复制代码
static

全局变量或整个步伐实行期间一连的生命周期。
  1. static HELLO: &str = "Hello";
复制代码
struct

定义一个结构体。
  1. struct Point {
  2.     x: i32,
  3.     y: i32,
  4. }
复制代码
super

当前模块的父模块。
  1. mod math {
  2.     pub fn add(x: i32, y: i32) -> i32 {
  3.         x + y
  4.     }
  5. }
  6. mod functions {
  7.     use super::math;
  8.     fn call_add() {
  9.         println!("3 + 4 = {}", math::add(3, 4));
  10.     }
  11. }
复制代码
trait

定义一个trait。
  1. trait Draw {
  2.     fn draw(&self);
  3. }
复制代码
true

布尔真字面量。
  1. let is_active: bool = true;
复制代码
type

定义一个类型别名或关联类型。
  1. type Result<T> = std::result::Result<T, std::io::Error>;
复制代码
union

定义一个团结体。
  1. union U {
  2.     x: i32,
  3.     y: f32,
  4. }
复制代码
unsafe

表现不安全代码、函数、trait或实现。
  1. unsafe fn dangerous_fn() {
  2.     // 不安全代码
  3. }
复制代码
use

将符号引入作用域。
  1. use std::io;
复制代码
where

表现约束类型的子句。
  1. fn generic_function<T>(t: T) where T: Copy {
  2.     // 使用T
  3. }
复制代码
while

根据表达式的结果有条件地循环。
  1. let mut number = 1;
  2. while number < 10 {
  3.     println!("{}", number);
  4.     number += 1;
  5. }
复制代码
为将来保留的关键字

以下是Rust为潜在的未来使用而保留的关键字,它们目前还没有功能:


  • abstract
  • become
  • box
  • do
  • final
  • macro
  • override
  • priv
  • try
  • typeof
  • unsized
  • virtual
  • yield
原始标识符

原始标识符是允许你在通常不允许使用关键字的地方使用关键字的语法。你可以通过在关键字前加上r#来使用原始标识符。
比方,match是一个关键字。如果你实行编译以下使用match作为其名称的函数:
  1. fn match(needle: &str, haystack: &str) -> bool {
  2.     haystack.contains(needle)
  3. }
复制代码
你会得到这个错误:
  1. error: expected identifier, found keyword `match`
  2. --> src/main.rs:4:4
  3.   |
  4. 4 | fn match(needle: &str, haystack: &str) -> bool {
  5.   |    ^^^^^ expected identifier, found keyword
复制代码
错误显示你不能使用关键字match作为函数标识符。要使用match作为函数名称,你需要使用原始标识符语法,如下所示:
  1. fn r#match(needle: &str, haystack: &str) -> bool {
  2.     haystack.contains(needle)
  3. }
  4. fn main() {
  5.     assert!(r#match("foo", "foobar"));
  6. }
复制代码
这段代码将无错误编译。注意在函数定义及其在main中被调用时函数名称上的r#前缀。
原始标识符允许你使用任何你选择的词作为标识符,即使这个词碰巧是一个保留的关键字。这为我们选择标识符名称提供了更多的自由,同时也让我们能够与用不同语言编写的步伐集成。别的,原始标识符允许你使用

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

半亩花草

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表