-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
983 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# 深入理解 Rust 所有权 | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Rust 语言引用和借用的艺术 | ||
|
||
--- | ||
|
||
Rust 是一门强调内存安全和并发性的现代化编程语言。其中一个强大的特性是引用和借用系统,它有效地防止了内存错误和数据竞争。在本教程中,我们将深入探讨 | ||
Rust 中引用和借用的概念,包括常见的引用 (&T)、可变引用、引用作用域等内容。让我们一起揭开这门语言的神秘面纱! | ||
|
||
## 概念概览 | ||
|
||
在 Rust 中,引用是一种轻量级的指针,允许你在不拥有所有权的情况下访问数据。这种借用的方式使得代码更加灵活和安全。 | ||
|
||
## 引用 (&T) | ||
|
||
Rust 中的引用使用`&`符号表示,它们允许你借用数据而不拥有它。这种方式使得多个部分可以同时访问相同的数据,而不会引发竞态条件。 | ||
|
||
```rust | ||
fn main() { | ||
let x = 42; | ||
let reference = &x; | ||
|
||
println!("x: {}", x); | ||
println!("Reference: {}", reference); | ||
} | ||
``` | ||
|
||
## 可变引用 | ||
|
||
当你需要修改数据时,Rust 允许你创建可变引用。可变引用使用`&mut`标记,并确保在特定作用域内只有一个可变引用。 | ||
|
||
```rust | ||
fn main() { | ||
let mut y = 24; | ||
let reference = &mut y; | ||
|
||
*reference += 1; | ||
|
||
println!("Modified y: {}", y); | ||
} | ||
``` | ||
|
||
## 引用作用域 | ||
|
||
Rust 引用的生命周期由其作用域决定。在引用作用域结束时,引用也会被销毁,从而避免悬垂引用问题。 | ||
|
||
```rust | ||
fn main() { | ||
let z = String::from("Hello"); | ||
|
||
{ | ||
let reference = &z; | ||
println!("Reference: {}", reference); | ||
} // reference 生命周期结束 | ||
|
||
// 下一行将会引发编译错误,因为 reference 已经超出作用域 | ||
// println!("Reference: {}", reference); | ||
} | ||
``` | ||
|
||
通过理解和灵活运用 Rust 的引用和借用系统,你可以写出更加安全和高效的代码。这只是一个引子,Rust | ||
的生态系统中还有许多其他有趣的概念等待你去探索。愿你在学习 Rust 的过程中收获满满,编写出令人印象深刻的程序! | ||
|
||
--- | ||
|
||
使用这篇教程,你可以深入了解 Rust 中引用和借用的基本概念,从而更好地掌握这门现代编程语言的核心特性。 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
# Rust 语言入门:从零开始掌握这门系统编程语言 | ||
# Rust 入门篇:从零开始掌握这门系统编程语言 | ||
|
||
--- | ||
|
||
## 安装 Rust 环境 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Rust 入门篇:语言基础概念 - Language Fundamentals | ||
|
||
--- | ||
|
||
Rust 是一种现代的、安全的系统编程语言,它结合了高效的性能和强大的类型系统,使得编写可靠和安全的软件变得更加容易。在本教程中,我们将介绍 | ||
Rust 语言的基础概念,包括变量、数据类型、函数、控制流等内容,并通过代码示例来加深理解。 | ||
|
||
### 变量和数据类型 | ||
|
||
在 Rust 中,变量可以使用`let`关键字进行声明,并且默认是不可变的。要创建可变变量,可以使用`mut`关键字。 | ||
|
||
```rust | ||
fn main() { | ||
// 不可变变量 | ||
let x = 5; | ||
println!("The value of x is: {}", x); | ||
|
||
// 可变变量 | ||
let mut y = 10; | ||
println!("The value of y is: {}", y); | ||
y = 15; | ||
println!("Now the value of y is: {}", y); | ||
} | ||
``` | ||
|
||
Rust 拥有丰富的内置数据类型,包括整数、浮点数、布尔值等。 | ||
|
||
```rust | ||
fn main() { | ||
// 整数类型 | ||
let a: i32 = 42; | ||
let b: u32 = 42; | ||
println!("The value of a is: {}", a); | ||
println!("The value of b is: {}", b); | ||
|
||
// 浮点数类型 | ||
let c: f64 = 3.14; | ||
println!("The value of c is: {}", c); | ||
|
||
// 布尔类型 | ||
let d: bool = true; | ||
println!("The value of d is: {}", d); | ||
} | ||
``` | ||
|
||
### 函数 | ||
|
||
函数是 Rust 中的基本构建块,可以使用`fn`关键字定义函数。 | ||
|
||
```rust | ||
fn main() { | ||
println!("The sum of 5 and 3 is: {}", add(5, 3)); | ||
} | ||
|
||
fn add(x: i32, y: i32) -> i32 { | ||
x + y | ||
} | ||
``` | ||
|
||
### 控制流 | ||
|
||
Rust 支持常见的控制流结构,如`if`表达式、`loop`循环、`while`循环和`for`循环。 | ||
|
||
```rust | ||
fn main() { | ||
let number = 7; | ||
|
||
// if 表达式 | ||
if number % 2 == 0 { | ||
println!("{} is even", number); | ||
} else { | ||
println!("{} is odd", number); | ||
} | ||
|
||
// while 循环 | ||
let mut counter = 0; | ||
while counter < 5 { | ||
println!("Counter: {}", counter); | ||
counter += 1; | ||
} | ||
|
||
// for 循环 | ||
let arr = [10, 20, 30, 40, 50]; | ||
for element in arr.iter() { | ||
println!("The value is: {}", element); | ||
} | ||
} | ||
``` | ||
|
||
### 结论 | ||
|
||
本教程介绍了 Rust 语言的基础概念,包括变量、数据类型、函数、控制流等内容。通过理解这些基础知识,并通过代码示例进行实践,您将对 | ||
Rust 编程有一个良好的起点。继续学习 Rust 语言的高级特性和最佳实践,将帮助您更加深入地掌握这门强大的编程语言。 | ||
|
||
希望本教程能够对您有所帮助! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Rust 入门篇:开发环境搭建 - Setting up Dev Environment | ||
|
||
--- | ||
|
||
Rust 是一种快速、安全、并发的系统编程语言,具有强大的类型系统和内存安全性。在本教程中,我们将介绍如何搭建 Rust | ||
的开发环境,以便开始编写和运行 Rust 程序。 | ||
|
||
### 步骤 1:安装 Rust | ||
|
||
首先,我们需要安装 Rust 编程语言的工具链。Rust 的官方工具链包含了 Rust 编译器(rustc)和包管理器(Cargo)。它们可以一起安装,因此只需执行一个命令即可。 | ||
|
||
在终端中运行以下命令(适用于 Unix 系统,如 Linux 和 macOS): | ||
|
||
```bash | ||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh | ||
``` | ||
|
||
或者,如果您使用的是 Windows 系统,请访问[Rust 官方网站](https://www.rust-lang.org/tools/install)下载安装程序并按照指示进行安装。 | ||
|
||
### 步骤 2:验证安装 | ||
|
||
安装完成后,可以通过以下命令验证 Rust 是否正确安装: | ||
|
||
```bash | ||
rustc --version | ||
cargo --version | ||
``` | ||
|
||
这将分别输出 Rust 编译器和 Cargo 包管理器的版本信息,确认安装成功。 | ||
|
||
### 步骤 3:创建并运行一个简单的 Rust 程序 | ||
|
||
现在让我们创建一个简单的 Rust 程序并运行它。 | ||
|
||
1. 打开您最喜欢的文本编辑器,创建一个新文件,命名为`hello.rs`。 | ||
|
||
2. 在文件中输入以下代码: | ||
|
||
```rust | ||
fn main() { | ||
println!("Hello, world!"); | ||
} | ||
``` | ||
|
||
3. 保存文件。 | ||
|
||
4. 在终端中,导航到保存`hello.rs`文件的目录。 | ||
|
||
5. 运行以下命令来编译并执行程序: | ||
|
||
```bash | ||
rustc hello.rs | ||
./hello | ||
``` | ||
|
||
你应该会在终端中看到输出`Hello, world!`。 | ||
|
||
### 步骤 4:使用 Cargo 创建项目 | ||
|
||
Cargo 是 Rust 的官方包管理器,它使得创建、构建和管理 Rust 项目变得更加简单。我们将使用 Cargo 来创建一个新的 Rust 项目。 | ||
|
||
1. 在终端中,导航到您选择的项目目录。 | ||
|
||
2. 运行以下命令来创建一个新的 Rust 项目: | ||
|
||
```bash | ||
cargo new my_project | ||
``` | ||
|
||
这将创建一个名为`my_project`的新目录,并在其中初始化一个新的 Rust 项目。 | ||
|
||
3. 进入新创建的项目目录: | ||
|
||
```bash | ||
cd my_project | ||
``` | ||
|
||
4. 在项目目录中,您将找到一个名为`Cargo.toml`的文件,它是项目的元数据文件,类似于其他语言中的`package.json` | ||
或`requirements.txt`文件。 | ||
|
||
5. 在终端中运行以下命令来构建并运行项目: | ||
|
||
```bash | ||
cargo run | ||
``` | ||
|
||
这将编译并运行项目中的默认程序(位于`src/main.rs`中)。 | ||
|
||
现在,您已经成功搭建了 Rust 的开发环境,并且可以开始编写和运行 Rust 程序了! | ||
|
||
--- | ||
|
||
这就是关于如何设置 Rust 开发环境的简单教程。希望这能帮助您顺利开始使用 Rust 编程。祝您编程愉快! |
Oops, something went wrong.