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

Rust 语法基础教程

Rust 语法基础教程

Rust 是一门系统编程语言,以内存安全、零成本抽象和并发安全著称。本文将介绍 Rust 的基础语法。

1. 变量与可变性

不可变变量

let x = 5;
// x = 6; // 错误:不能修改不可变变量

可变变量

let mut y = 5;
y = 6; // 正确:可以修改可变变量

常量

const MAX_POINTS: u32 = 100_000;

变量遮蔽 (Shadowing)

let x = 5;
let x = x + 1; // 遮蔽前一个 x
let x = x * 2; // 再次遮蔽

2. 数据类型

标量类型

// 整数类型
let a: i32 = 42;
let b: u64 = 100;// 浮点类型
let c: f64 = 3.14;
let d = 2.0; // f64 默认类型// 布尔类型
let e: bool = true;
let f = false;// 字符类型
let g: char = 'z';
let h = '😻';

复合类型

// 元组
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup; // 解构
let first = tup.0; // 索引访问// 数组
let arr: [i32; 5] = [1, 2, 3, 4, 5];
let same_value = [3; 5]; // [3, 3, 3, 3, 3]
let first_element = arr[0];

3. 函数

// 基本函数
fn greet(name: &str) {println!("Hello, {}!", name);
}// 带返回值的函数
fn add(a: i32, b: i32) -> i32 {a + b // 表达式,无分号
}// 多返回值
fn calculate(x: i32, y: i32) -> (i32, i32) {(x + y, x - y)
}

4. 控制流

if 表达式

let number = 6;if number % 4 == 0 {println!("number is divisible by 4");
} else if number % 3 == 0 {println!("number is divisible by 3");
} else {println!("number is not divisible by 4 or 3");
}// if 作为表达式
let condition = true;
let number = if condition { 5 } else { 6 };

循环

// loop 循环
let mut counter = 0;
let result = loop {counter += 1;if counter == 10 {break counter * 2;}
};// while 循环
let mut number = 3;
while number != 0 {println!("{}!", number);number -= 1;
}// for 循环
let a = [10, 20, 30, 40, 50];
for element in a.iter() {println!("the value is: {}", element);
}// 范围循环
for number in 1..4 {println!("{}!", number);
}

5. 所有权 (Ownership)

基本规则

  1. Rust 中的每一个值都有一个被称为其所有者的变量
  2. 值在任一时刻有且只有一个所有者
  3. 当所有者离开作用域,这个值将被丢弃
// 移动 (Move)
let s1 = String::from("hello");
let s2 = s1; // s1 被移动到 s2,s1 不再有效// 克隆 (Clone)
let s1 = String::from("hello");
let s2 = s1.clone(); // 深拷贝// 引用 (References)
let s1 = String::from("hello");
let len = calculate_length(&s1); // 借用fn calculate_length(s: &String) -> usize {s.len()
}

6. 结构体 (Struct)

// 定义结构体
struct User {username: String,email: String,sign_in_count: u64,active: bool,
}// 创建实例
let user1 = User {email: String::from("someone@example.com"),username: String::from("someusername123"),active: true,sign_in_count: 1,
};// 元组结构体
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);// 方法
impl User {fn new(email: String, username: String) -> User {User {email,username,active: true,sign_in_count: 1,}}fn is_active(&self) -> bool {self.active}
}

7. 枚举 (Enum)

// 基本枚举
enum IpAddrKind {V4,V6,
}// 带数据的枚举
enum IpAddr {V4(u8, u8, u8, u8),V6(String),
}// Option 枚举
let some_number = Some(5);
let some_string = Some("a string");
let absent_number: Option<i32> = None;// match 表达式
fn value_in_cents(coin: Coin) -> u8 {match coin {Coin::Penny => 1,Coin::Nickel => 5,Coin::Dime => 10,Coin::Quarter => 25,}
}

8. 错误处理

// Result 类型
use std::fs::File;
use std::io::ErrorKind;let f = File::open("hello.txt");
let f = match f {Ok(file) => file,Err(error) => match error.kind() {ErrorKind::NotFound => match File::create("hello.txt") {Ok(fc) => fc,Err(e) => panic!("Problem creating the file: {:?}", e),},other_error => panic!("Problem opening the file: {:?}", other_error),},
};// unwrap 和 expect
let f = File::open("hello.txt").unwrap();
let f = File::open("hello.txt").expect("Failed to open hello.txt");

9. 泛型、Trait 和生命周期

// 泛型函数
fn largest<T: PartialOrd + Copy>(list: &[T]) -> T {let mut largest = list[0];for &item in list {if item > largest {largest = item;}}largest
}// Trait 定义
trait Summary {fn summarize(&self) -> String;
}// Trait 实现
struct NewsArticle {headline: String,content: String,
}impl Summary for NewsArticle {fn summarize(&self) -> String {format!("{}: {}", self.headline, self.content)}
}

总结

Rust 的语法设计注重安全性和性能,通过所有权系统、类型系统和借用检查器确保内存安全。掌握这些基础语法是学习 Rust 的重要第一步。关于泛型,trait,所有权,生命周期在后面会详细讲解

http://www.dtcms.com/a/334040.html

相关文章:

  • sqli-labs通关笔记-第52关 GET数值型order by堆叠注入(手工注入+脚本注入两种方法)
  • Ubuntu 25.04 安装并使用 MySQL 8.4.5 的步骤
  • 使用 npm-run-all2 简化你的 npm 脚本工作流
  • Linux中的restore
  • PHP域名授权系统网站源码/授权管理工单系统/精美UI/附教程
  • 集成电路学习:什么是Haar Cascade Classifier Haar级联分类器
  • Vue 3.5+ Teleport defer 属性详解:解决组件渲染顺序问题的终极方案
  • JavaScript 实用工具方法小全
  • 在STM32F103上进行FreeRTOS移植和配置(源码移植)
  • 【总结型】c语言中的位运算
  • 给AI装上“稳压器”:批归一化如何加速深度学习
  • [Linux] Cockpit管理服务器 软件包管理
  • VScode 使用遇到的问题
  • linux docker neo4j 导出 (windows 导入)
  • Winsows系统去除右键文件显示的快捷列表
  • 微服务架构实战指南:从单体应用到云原生的蜕变之路
  • 头文件包含和前置声明
  • python---包
  • libcurl 中 curl_multi 的演进:从双路并进到 epoll 革命
  • 8.16打卡 DAY43 复习日
  • 0301-solidity进阶-区块链-web3
  • 布隆过滤器的原理及使用
  • Kotlin-基础语法练习一
  • Maven私服配置模版
  • Qt 关于QString和std::string数据截断的问题- 遇到\0或者0x00如何处理?
  • 小白学投资理财 第一天
  • 算力与显存、显存带宽的关系
  • 【php反序列化介绍与常见触发方法】
  • Houdini Vop学习笔记
  • 测试工程师的AI转型指南:从工具使用到测试策略重构