马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Rust是一种注重安全性、并发性和性能的系统编程语言。在Rust中,关键字是保留的标识符,用于语言的特定语法结构。这些关键字不能用作普通的标识符,除非使用原始标识符(raw identifiers)。下面,我们将通过实例详细先容Rust中当前使用的关键字及其功能。
当前使用的关键字及着实例
as
用于实行原始类型转换或消除trait中条目标歧义。
- let x: i32 = 5;
- let y: f64 = x as f64; // 将i32类型的x转换为f64类型
复制代码 async
返回一个Future而不是阻塞当前线程。
- async fn async_function() {
- // 异步代码
- }
复制代码 await
挂起实行直到Future的结果准备好。
- async fn get_data() -> String {
- async {
- "Data".to_string()
- }
- .await
- }
复制代码 break
立即退出循环。
- for i in 0..10 {
- if i == 5 {
- break; // 当i等于5时退出循环
- }
- }
复制代码 const
定义常量项。
- const MAX_POINTS: u32 = 100_000;
复制代码 continue
继续到下一个循环迭代。
- for i in 0..10 {
- if i % 2 == 0 {
- continue; // 跳过偶数,继续下一次循环
- }
- println!("{}", i);
- }
复制代码 crate
在模块路径中,指的是crate根。
dyn
动态派送到trait对象。
- trait Draw {
- fn draw(&self);
- }
- struct Screen;
- impl Draw for Screen {
- fn draw(&self) {
- println!("Drawing screen");
- }
- }
- let screen: &dyn Draw = &Screen;
- screen.draw();
复制代码 else
if和if let控制流构造的备选方案。
- let condition = true;
- if condition {
- println!("Condition is true");
- } else {
- println!("Condition is false");
- }
复制代码 enum
定义一个枚举。
- enum Message {
- Quit,
- Move { x: i32, y: i32 },
- Write(String),
- }
复制代码 extern
链接一个外部函数或变量。
- extern crate rand;
- use rand::Rng;
- let mut rng = rand::thread_rng();
- let n: u32 = rng.gen();
复制代码 false
布尔假字面量。
- let is_active: bool = false;
复制代码 fn
定义一个函数。
- fn double(x: i32) -> i32 {
- x * 2
- }
复制代码 for
循环遍历迭代器中的项。
- for item in vec![1, 2, 3] {
- println!("{}", item);
- }
复制代码 if
根据条件表达式的结果进行分支。
- let condition = true;
- if condition {
- println!("Condition is true");
- }
复制代码 impl
实现固有或trait功能。
- struct Rectangle {
- width: u32,
- height: u32,
- }
- impl Rectangle {
- fn area(&self) -> u32 {
- self.width * self.height
- }
- }
复制代码 in
for循环语法的一部门。
- for i in 0..10 {
- println!("{}", i);
- }
复制代码 let
绑定一个变量。
loop
无条件循环。
- loop {
- println!("Infinite loop");
- break; // 需要显式break退出
- }
复制代码 match
将值匹配到模式。
- let num = Some(4);
- match num {
- Some(x) => println!("Num is {}", x),
- None => println!("No num"),
- }
复制代码 mod
定义一个模块。
- mod math_functions {
- pub fn add(x: i32, y: i32) -> i32 {
- x + y
- }
- }
复制代码 move
使闭包取得其捕获的全部全部权。
- let text = "Hello".to_string();
- let move_text = move || {
- println!("{}", text);
- };
- move_text();
复制代码 mut
在引用、原始指针或模式绑定中表现可变性。
- let mut num = 5;
- num += 1;
复制代码 pub
在结构体字段、impl块或模块中表现公共可见性。
- pub struct Point {
- pub x: i32,
- pub y: i32,
- }
复制代码 ref
通过引用绑定。
- let x = 5;
- let y: &i32 = &x;
复制代码 return
从函数返回。
- fn double(x: i32) -> i32 {
- return x * 2;
- }
复制代码 Self
我们正在定义或实现的类型的类型别名。
- impl Rectangle {
- fn area(&self) -> u32 {
- self.width * self.height
- }
- }
复制代码 self
方法主体或当前模块。
- impl Rectangle {
- fn area(&self) -> u32 {
- self.width * self.height
- }
- }
复制代码 static
全局变量或整个步伐实行期间一连的生命周期。
- static HELLO: &str = "Hello";
复制代码 struct
定义一个结构体。
- struct Point {
- x: i32,
- y: i32,
- }
复制代码 super
当前模块的父模块。
- mod math {
- pub fn add(x: i32, y: i32) -> i32 {
- x + y
- }
- }
- mod functions {
- use super::math;
- fn call_add() {
- println!("3 + 4 = {}", math::add(3, 4));
- }
- }
复制代码 trait
定义一个trait。
- trait Draw {
- fn draw(&self);
- }
复制代码 true
布尔真字面量。
- let is_active: bool = true;
复制代码 type
定义一个类型别名或关联类型。
- type Result<T> = std::result::Result<T, std::io::Error>;
复制代码 union
定义一个团结体。
- union U {
- x: i32,
- y: f32,
- }
复制代码 unsafe
表现不安全代码、函数、trait或实现。
- unsafe fn dangerous_fn() {
- // 不安全代码
- }
复制代码 use
将符号引入作用域。
where
表现约束类型的子句。
- fn generic_function<T>(t: T) where T: Copy {
- // 使用T
- }
复制代码 while
根据表达式的结果有条件地循环。
- let mut number = 1;
- while number < 10 {
- println!("{}", number);
- number += 1;
- }
复制代码 为将来保留的关键字
以下是Rust为潜在的未来使用而保留的关键字,它们目前还没有功能:
- abstract
- become
- box
- do
- final
- macro
- override
- priv
- try
- typeof
- unsized
- virtual
- yield
原始标识符
原始标识符是允许你在通常不允许使用关键字的地方使用关键字的语法。你可以通过在关键字前加上r#来使用原始标识符。
比方,match是一个关键字。如果你实行编译以下使用match作为其名称的函数:
- fn match(needle: &str, haystack: &str) -> bool {
- haystack.contains(needle)
- }
复制代码 你会得到这个错误:
- error: expected identifier, found keyword `match`
- --> src/main.rs:4:4
- |
- 4 | fn match(needle: &str, haystack: &str) -> bool {
- | ^^^^^ expected identifier, found keyword
复制代码 错误显示你不能使用关键字match作为函数标识符。要使用match作为函数名称,你需要使用原始标识符语法,如下所示:
- fn r#match(needle: &str, haystack: &str) -> bool {
- haystack.contains(needle)
- }
- fn main() {
- assert!(r#match("foo", "foobar"));
- }
复制代码 这段代码将无错误编译。注意在函数定义及其在main中被调用时函数名称上的r#前缀。
原始标识符允许你使用任何你选择的词作为标识符,即使这个词碰巧是一个保留的关键字。这为我们选择标识符名称提供了更多的自由,同时也让我们能够与用不同语言编写的步伐集成。别的,原始标识符允许你使用
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |