Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化代码 #24

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions 02_BaseType/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

8 changes: 5 additions & 3 deletions 02_BaseType/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ fn main() {
let c_char = 'z';
let z_char = 'ℤ';
let heart_eyed_cat_char = '😻';
let y_char = '棒';
let z_string = String::from("棒");
println!("c_char is {}, z_char is {}, heart_eyed_cat_char is {}", c_char, z_char, heart_eyed_cat_char);
println!("字符'c_char'占用了{}字节的内存大小", std::mem::size_of_val(&c_char));
println!("c_char is {}, z_char is {}, heart_eyed_cat_char is {}, y_char is {}", c_char, z_char, heart_eyed_cat_char, y_char);
println!("字符'c_char'占用了{}字节的内存大小", size_of_val(&c_char));
println!("字符'y_char'占用了{}字节的内存大小", size_of_val(&y_char));
println!("字符串'z_string'内容占用了{}字节的内存大小", &z_string.as_bytes().len());

let tup_var: (i32, f64, u8, char) = (-500, 6.4, 1, 'z');
Expand All @@ -36,4 +38,4 @@ fn main() {
let first_element = a_array[0];
let second_element = a_array[1];
println!("The first element is {}, the second element is {}", first_element, second_element);
}
}
2 changes: 1 addition & 1 deletion 03_CompoundType/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
itertools = "0.10"

5 changes: 1 addition & 4 deletions 03_CompoundType/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use itertools::Itertools;

fn main() {
let mixed = ("Rust", 2023, 3.14, true);
let (lang, year, pi, status) = mixed;
Expand All @@ -15,7 +13,7 @@ fn main() {
println!("vector v1: {:?}", &v1);

// 2.使用宏 vec! 来创建数组,支持在创建时就给予初始化值
let v2 = vec![1u8, 2, 3];
let v2 = vec![1u8, 2, 3];
println!("vector v2: {:?}", &v2);

let mut s = String::from("Hello"); // 可变的String类型
Expand All @@ -27,7 +25,6 @@ fn main() {


let numbers = [1, 2, 3, 4, 5];
// 给数组 numbers 增加数字 6和7

let slice = &numbers[0..2]; // 引用数组的一部分

Expand Down
3 changes: 1 addition & 2 deletions 04_Struct_Enum/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// 定义一个Person结构体
struct Person {
name: String,
Expand Down Expand Up @@ -28,4 +27,4 @@ fn main() {

// 调用这个函数,不需要 owner
show_info();
}
}
5 changes: 3 additions & 2 deletions 05_Ownership/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ fn calculate_length(s: &String) -> usize {
s.len()
}

fn change(some_string: &mut String) -> &mut String{
fn change(some_string: &mut String) -> &mut String {
some_string.push_str(", wtf!");
some_string
}

fn main() {
let mut s1 = String::from("hello"); // s1 是 hello 对象的所有者
s1.push_str(", wtf!"); // s1 可以修改 hello 对象,追加字符串
Expand All @@ -30,4 +31,4 @@ fn main() {
let mut s4 = String::from("hello");
change(&mut s4); // s4 发生可变借用,函数可以修改 s4
println!("{}", s4);
}
}
4 changes: 2 additions & 2 deletions 06_Function/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn print_sum(a: i32, b: i32) {
}

fn add_two(a: i32) -> i32 {
return a + 2
a + 2
// 或 `return a + 2;`
}

Expand Down Expand Up @@ -63,4 +63,4 @@ fn main() {
break;
}
}
}
}
Loading