Learning
This commit is contained in:
6
BasicSyntax/HelloWorld/Cargo.toml
Normal file
6
BasicSyntax/HelloWorld/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "test1"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
BIN
BasicSyntax/HelloWorld/main.exe
Normal file
BIN
BasicSyntax/HelloWorld/main.exe
Normal file
Binary file not shown.
BIN
BasicSyntax/HelloWorld/main.pdb
Normal file
BIN
BasicSyntax/HelloWorld/main.pdb
Normal file
Binary file not shown.
3
BasicSyntax/HelloWorld/src/main.rs
Normal file
3
BasicSyntax/HelloWorld/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
2086
BasicSyntax/get-dependencies/Cargo.lock
generated
Normal file
2086
BasicSyntax/get-dependencies/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
8
BasicSyntax/get-dependencies/Cargo.toml
Normal file
8
BasicSyntax/get-dependencies/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "get-dependencies"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
||||
trpl = "0.2.0"
|
||||
3
BasicSyntax/get-dependencies/src/main.rs
Normal file
3
BasicSyntax/get-dependencies/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
44
BasicSyntax/readme.md
Normal file
44
BasicSyntax/readme.md
Normal file
@@ -0,0 +1,44 @@
|
||||
太好了!我给你整理一个 **Rust 基础语法清单(中英对照)**,涵盖你入门阶段最关键的概念和语法点,方便快速查阅和记忆。
|
||||
|
||||
---
|
||||
|
||||
# Rust 基础语法清单(中英对照)
|
||||
|
||||
| 中文概念 | 英文术语 | 示例代码 | 说明 |
|
||||
| ---------- | ------------------------ | ---------------------------------------------------- | ----------------------- |
|
||||
| 变量绑定 | Variable binding | `let x = 5; let mut y = 6;` | 默认不可变,用 `mut` 可变 |
|
||||
| 常量 | Constant | `const MAX_POINTS: u32 = 100_000;` | 必须标注类型,永远不可变 |
|
||||
| 数据类型 | Data types | `i32, u32, f64, bool, char, String` | Rust 是强类型语言 |
|
||||
| 函数 | Function | `fn add(a: i32, b: i32) -> i32 { a + b }` | 参数类型和返回类型必须标注 |
|
||||
| 表达式 & 语句 | Expressions & Statements | `let y = { let x = 3; x + 1 };` | Rust 区分表达式(有值)和语句(无值) |
|
||||
| 控制流 | Control flow | `if, else, loop, while, for` | 语法类似 C,但更安全 |
|
||||
| 所有权 | Ownership | `let s1 = String::from("hello"); let s2 = s1;` | 值的所有权转移后,原变量不可再用 |
|
||||
| 借用 | Borrowing | `let s2 = &s1; let mut s3 = &mut s1;` | `&T` 只读借用,`&mut T` 可写借用 |
|
||||
| 生命周期 | Lifetimes | `fn longest<'a>(x: &'a str, y: &'a str) -> &'a str` | 保证引用在有效范围内 |
|
||||
| 数组 | Array | `let a = [1, 2, 3, 4];` | 固定长度,类型一致 |
|
||||
| 切片 | Slice | `let s = &a[1..3];` | 指向数组部分数据的引用 |
|
||||
| 元组 | Tuple | `let t: (i32, f64, char) = (1, 2.0, 'a');` | 可存储不同类型的组合 |
|
||||
| 枚举 | Enum | `enum Direction { Up, Down, Left, Right }` | 用于模式匹配和状态表示 |
|
||||
| 结构体 | Struct | `struct Person { name: String, age: u8 }` | 自定义数据类型 |
|
||||
| 模式匹配 | Match | `match value { 1 => "one", _ => "other" }` | 强大的分支选择工具 |
|
||||
| Trait(接口) | Trait | `trait Speak { fn speak(&self); }` | 定义行为规范,类似接口 |
|
||||
| 泛型 | Generics | `fn largest<T: PartialOrd>(list: &[T]) -> T { ... }` | 支持类型参数化 |
|
||||
| 错误处理 | Error handling | `Result<T, E>`, `Option<T>` | 安全替代异常机制 |
|
||||
| 智能指针 | Smart pointer | `Box<T>, Rc<T>, Arc<T>, RefCell<T>` | 管理堆上数据和共享所有权 |
|
||||
| 模块 & crate | Modules & Crates | `mod my_mod; use my_mod::func;` | 组织代码和依赖管理 |
|
||||
| 宏 | Macro | `println!("Hello {}", name);` | 编译时代码生成,类似模板 |
|
||||
| 注释 | Comment | `// 单行` `/* 多行 */` | 普通注释和文档注释 `///` |
|
||||
|
||||
---
|
||||
|
||||
💡 **学习建议**:
|
||||
|
||||
1. 先用 **变量、函数、if/loop、数组/元组、枚举、结构体** 搭建小项目。
|
||||
2. 再深入 **所有权、借用、生命周期、错误处理**,这些是 Rust 的核心。
|
||||
3. 最后学习 **Trait、泛型、智能指针**,能写出可复用、高性能的 Rust 代码。
|
||||
|
||||
---
|
||||
|
||||
如果你愿意,我可以帮你做一个 **Rust 基础语法思维导图**,把这些概念从“基础 → 核心特性 → 高级特性”可视化,非常适合快速复习。
|
||||
|
||||
你希望我帮你做吗?
|
||||
6
BasicSyntax/test1/Cargo.toml
Normal file
6
BasicSyntax/test1/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "test1"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
BIN
BasicSyntax/test1/src/apple.exe
Normal file
BIN
BasicSyntax/test1/src/apple.exe
Normal file
Binary file not shown.
BIN
BasicSyntax/test1/src/apple.pdb
Normal file
BIN
BasicSyntax/test1/src/apple.pdb
Normal file
Binary file not shown.
21
BasicSyntax/test1/src/apple.rs
Normal file
21
BasicSyntax/test1/src/apple.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use std::io;
|
||||
|
||||
fn pr(a:i64){
|
||||
if a > 1 {
|
||||
println!("Today, I ate {} apples.", a)
|
||||
}
|
||||
else if a == 1 || a == 0 {
|
||||
println!("Today, I ate {} apple.", a)
|
||||
}
|
||||
}
|
||||
|
||||
fn main(){
|
||||
let mut input = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("");
|
||||
|
||||
let mut a = input.trim().parse::<i64>().expect("");
|
||||
pr(a)
|
||||
|
||||
}
|
||||
BIN
BasicSyntax/test1/src/main.exe
Normal file
BIN
BasicSyntax/test1/src/main.exe
Normal file
Binary file not shown.
BIN
BasicSyntax/test1/src/main.pdb
Normal file
BIN
BasicSyntax/test1/src/main.pdb
Normal file
Binary file not shown.
27
BasicSyntax/test1/src/main.rs
Normal file
27
BasicSyntax/test1/src/main.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use std::io;
|
||||
|
||||
fn add(a:i64, b:i64) -> i64 {
|
||||
return a + b ;
|
||||
}
|
||||
|
||||
fn main(){
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("");
|
||||
|
||||
// 用空格拆分字符串
|
||||
let nums: Vec<i64> = input
|
||||
.trim() // 去掉换行符
|
||||
.split_whitespace() // 按空格拆分
|
||||
.map(|s| s.parse::<i64>().expect("请输入有效整数")) // 转换为整数
|
||||
.collect();
|
||||
|
||||
// 假设用户输入了两个整数
|
||||
let a = nums[0];
|
||||
let b = nums[1];
|
||||
|
||||
let c = add(a, b);
|
||||
println!("{}", c);
|
||||
}
|
||||
22
BasicSyntax/test1/src/two.rs
Normal file
22
BasicSyntax/test1/src/two.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use std::io;
|
||||
|
||||
fn add(a : i64, b : i64) -> i64 {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
fn main(){
|
||||
let mut sa = String::new();
|
||||
let mut sb = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut sa)
|
||||
.expect("");
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut sb)
|
||||
.expect("");
|
||||
|
||||
let a: i64 = sa.trim().parse::<i64>().expect("");
|
||||
let b: i64 = sb.trim().parse::<i64>().expect("");
|
||||
let c = add(a, b);
|
||||
println!("{}", c);
|
||||
}
|
||||
Reference in New Issue
Block a user