-
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.
Merge pull request #2 from HungYann/homework3
第三课:复合类型
- Loading branch information
Showing
2 changed files
with
193 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,102 @@ | ||
|
||
## 笔记一 | ||
|
||
对于不固定的字节类型, 会默认放在堆上(成为堆上的一个资源) | ||
元组结构体:所谓元组结构体,也就是元组和结构体的结合体 | ||
|
||
```rust | ||
fn main() { | ||
let s1 = String::from("I am a superman"); | ||
|
||
let s2 = s1; | ||
|
||
println!("{s1}"); | ||
struct Color(i32, i32); | ||
|
||
println!("{s2}"); | ||
fn main() { | ||
let _black = Color(0, 0); | ||
} | ||
``` | ||
|
||
什么是所有权? | ||
|
||
浅拷贝/内存安全:默认只进行值复制 ,rust中默认只保留最新的s2到字符串的指向。 | ||
同时抹除s1到字符串的指向。 | ||
|
||
Rust中,每一个值(资源)都有一个所有者。 | ||
一句话解释:谁(获得着)控制这个数据的生死权利 | ||
|
||
任何一个时刻,一个值只有一个所有者。 | ||
在Rust中,每一个值都有一个决定其生命周期的唯一所有者(owner) | ||
|
||
当所有者所在任何域结束的时候,值会被释放掉。 | ||
所有权的存在原因? | ||
|
||
- 跟踪代码的哪些部分正在使用heap的哪些数据 | ||
- 最小化heap上的重复数据 | ||
- 清理heap上未使用的数据以免空间不足 | ||
|
||
## 笔记二 | ||
|
||
`struct`结构体 | ||
|
||
**移动还是复制** | ||
|
||
默认做复制操作的有: | ||
|
||
所有的整数类型:比如u32 | ||
|
||
布尔类型:bool | ||
|
||
浮点类型:f32 、 f64 | ||
|
||
字符串类型:char | ||
|
||
由以上类型组成的tuple | ||
|
||
|
||
## 笔记三 | ||
|
||
所有权 | ||
|
||
Rust中,每一个值(资源)都有一个所有者; | ||
|
||
任何一个时候,一个值只有一个所有者; | ||
|
||
当所有者所在作用域结束的时候,值会被释放掉; | ||
|
||
|
||
```rust | ||
let b = &a; | ||
```rust | ||
struct User { | ||
active: bool, | ||
username: String, | ||
email: String, | ||
sign_in_count: u32, | ||
} | ||
|
||
let c = b; | ||
fn main() { | ||
let active = true; | ||
let username = String::from("someusername123"); | ||
let email = String::from("someuser@example123"); | ||
let user1 = User { | ||
active, | ||
username, | ||
email, | ||
sign_in_count: 1, | ||
}; | ||
|
||
let user2 = User { | ||
email: String::from("[email protected]"), | ||
..user1 | ||
}; | ||
} | ||
``` | ||
|
||
`enum`枚举 | ||
|
||
|
||
`if let` | ||
```rust | ||
fn main() { | ||
let a = 10u32; | ||
|
||
let b = &a; | ||
|
||
let c = &&&&&a; | ||
|
||
let d = &b; | ||
|
||
let e = b; | ||
|
||
println!("{a}"); | ||
|
||
println!("{b}"); | ||
|
||
println!("{c}"); | ||
|
||
println!("{d}"); | ||
|
||
println!("{e}"); | ||
let mut optional = Some(0); | ||
|
||
if let Some(i) = optional { | ||
if i > 9 { | ||
println!("Greater than 9, quit"); | ||
optional = None; | ||
} else { | ||
println!("t"); | ||
} | ||
} | ||
} | ||
|
||
|
||
``` | ||
|
||
|
||
10 | ||
10 | ||
10 | ||
10 | ||
10 | ||
`Vec`与HashMap | ||
|
||
```rust | ||
fn foo1(s: &str) { | ||
|
||
} | ||
|
||
## 笔记四 | ||
fn foo2(s: &[u32]) { | ||
|
||
引用的作用域是从它定义到它最后一次使用时结束 | ||
} | ||
|
||
fn main() { | ||
let s = String::from("aaa"); | ||
foo1(&s); | ||
|
||
## 笔记五 | ||
foo1("aaaabbb"); | ||
|
||
用&和&mut来改进函数的定义 | ||
let v: Vec<u32> = vec![1, 2, 3, 4, 5]; | ||
foo2(&v); | ||
|
||
如果一个函数参数接受的是可变引用,或所有权参数,那么它 | ||
里面的逻辑一般都会对引用的资源进行修改。如果一个函数 | ||
参数只接受不可变引用,那么它里面的逻辑,就一定不会修改 | ||
被引用的资源。 | ||
foo2(&[1, 2, 3, 4, 5]); | ||
} | ||
``` | ||
|
||
|
||
|
||
|
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,8 +1,134 @@ | ||
fn foo(s: &String) { | ||
println!("in fo") | ||
// 添加学生 | ||
#[derive(Debug)] | ||
struct Student { | ||
id: u32, | ||
name: String, | ||
age: u32, | ||
grade: u32, | ||
class_id: u32, | ||
} | ||
|
||
fn main() { | ||
#[derive(Debug)] | ||
struct ClassInfo { | ||
id: u32, | ||
name: String, | ||
description: String, | ||
} | ||
|
||
struct StudentManager { | ||
students: Vec<Student>, | ||
classes: Vec<ClassInfo>, | ||
} | ||
|
||
impl StudentManager { | ||
// 创建一个新的StudentManager | ||
fn new() -> Self { | ||
StudentManager { | ||
students: Vec::new(), | ||
classes: Vec::new() | ||
} | ||
} | ||
|
||
// 创建一个新的学生 | ||
fn create_student(&mut self, id: u32, name: &str, age: u32, grade: u32, class_id: u32) { | ||
let student = Student{ | ||
id, | ||
name: name.to_string(), | ||
age, | ||
grade, | ||
class_id, | ||
}; | ||
|
||
self.students.push(student); | ||
} | ||
|
||
// 创建一个新的班级 | ||
fn create_class(&mut self, id: u32, name: &str, description: &str) { | ||
let class_info = ClassInfo{ | ||
id, | ||
name: name.to_string(), | ||
description: description.to_string(), | ||
}; | ||
self.classes.push(class_info); | ||
} | ||
|
||
// 根据id查找学生 | ||
fn get_student(&self, id: u32) -> Option<&Student> { | ||
self.students.iter().find(|s| s.id == id) | ||
} | ||
|
||
// 根据id查找班级 | ||
fn get_class(&self, id: u32) -> Option<&ClassInfo> { | ||
self.classes.iter().find(|c| c.id == id) | ||
} | ||
|
||
// 更新学生信息 | ||
fn update_student(&mut self, id: u32, name: &str, age: u32, grade: u32, class_id: u32) { | ||
if let Some(student) = self.students.iter_mut().find(|s| s.id == id) { | ||
student.name = name.to_string(); | ||
student.age = age; | ||
student.grade = grade; | ||
student.class_id = class_id; | ||
} | ||
} | ||
|
||
// 更新班级信息 | ||
fn update_class(&mut self, id: u32, name: &str, description: &str) { | ||
if let Some(class_info) = self.classes.iter_mut().find(|c| c.id == id) { | ||
class_info.name = name.to_string(); | ||
class_info.description = description.to_string(); | ||
} | ||
} | ||
|
||
// 根据id删除学生 | ||
fn delete_student(&mut self, id: u32) { | ||
self.students.retain(|s| s.id != id); | ||
} | ||
|
||
// 根据id删除班级 | ||
fn delete_class(&mut self, id: u32) { | ||
self.classes.retain(|c| c.id != id); | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut student_manager = StudentManager::new(); | ||
|
||
// 创建班级 | ||
student_manager.create_class(1, "Rust课程" , "Rust学习课程"); | ||
student_manager.create_class(2, "Move课程" , "Move学习课程"); | ||
|
||
// 创建学生 | ||
student_manager.create_student(1, "Lehy", 20, 90, 1); | ||
|
||
student_manager.create_student(2, "Bob", 21, 85, 1); | ||
|
||
student_manager.create_student(3, "Nancy", 20, 92, 2); | ||
|
||
// 查找学生和班级 | ||
// 查找学生和班级 | ||
if let Some(student) = student_manager.get_student(1) { | ||
println!("Found student: {:?}", student); | ||
} | ||
if let Some(class_info) = student_manager.get_class(1) { | ||
println!("Found class: {:?}", class_info); | ||
} | ||
|
||
// 更新学生和班级信息 | ||
student_manager.update_student(2, "Bob Simith", 22, 88, 2); | ||
student_manager.update_class(1, "Rust课程", "Rust课程更新" ); | ||
|
||
println!("更新数据后:"); | ||
// 查找学生和班级 | ||
// 查找学生和班级 | ||
if let Some(student) = student_manager.get_student(2) { | ||
println!("Found student: {:?}", student); | ||
} | ||
if let Some(class_info) = student_manager.get_class(1) { | ||
println!("Found class: {:?}", class_info); | ||
} | ||
|
||
// 删除学生和班级 | ||
student_manager.delete_student(2); | ||
student_manager.delete_class(2); | ||
} |