学习rust_day05

切片

例:

1
2
3
4
5
6
fn main() {
let s = String::from("Hello World");

let hello: &str = &s[0..5]; //存储"Hello"
let world: &str = &s[6..11]; //存储"World"
}

结构体

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}

fn main() {
let mut user1 = User {
email: String::from("c@cc.com"),
username: String::from("cc"),
active: true,
sign_in_count: 1,
};

let user2 = User {
email: String::from("k@kk.com"),
..user1 //简便写法
};

user1.email = String::from("a@aa.com");
}

fn build_user(email: String, username: String) -> User {
User {
active: true,
username, //简便写法
email, //简便写法
sign_in_count: 1,
}
}

元组结构体

例:

1
2
3
4
5
6
7
8
struct Color(i32, i32, i32);

struct Point(i32, i32, i32);

fn main() {
let black = Color(0, 0, 0);
let orange = Point(0, 0, 0);
}

为结构体实现方法

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct Box{
width: u32,
height: u32,
}

impl Box {
fn area(&self) -> u32 {
self.width * self.height
}

fn square(size: u32) -> Self { //返回一个正方形
Self {
width: size,
height: size,
}
}
}

fn main() {
let first = Box {
width: 10,
height: 15,
};

println!("{}", first.area());
}

枚举

例:

1
2
3
4
5
6
7
8
9
enum IpAddrKind {
V4(u8, u8, u8, u8),
V6(String),
}

fn main() {
let pad = IpAddrKind::V4(127, 0, 0, 1);
let phone = IpAddrKind::V6(String::from("::1"));
}

为枚举定义方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
enum IpAddrKind {
V4(u8, u8, u8, u8),
V6(String),
}

fn main() {
let pad = IpAddrKind::V4(127, 0, 0, 1);
let _phone = IpAddrKind::V6(String::from("::1"));
pad.fun();
}

impl IpAddrKind {
fn fun(& self) {
println!("Hello");
}
}

match表达式与模式匹配

例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() {
let five = Some(5);
let six = fun(five); //返回Some(6)
let none = fun(None); //返回None

let one = Some(1u8);
if let Some(max) = one {
println!("{max}");
} else {
println!("None");
}
}

fn fun(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
_ => { //表示处此之外其他任何可能性,需要变量可以自行增加例如 other =>
println!("No Way");
None
},
}
}

项目代码组织

crate
crate是组织和共享代码的基本构建块

  • binary crate:可执行的,需有main函数
  • library crate:没有main函数,无法执行。定义一些功能,可共享使用。

crate root
编译crate的入口点(源代码文件)

  • binary crate:src/main.rs
  • library crate:src/lib.rs

package
由1个或多个crates组成
包含Cargo.toml文件(描述了如何构建这些crates)
package的规则:

  • 可有多个binary crates
  • 最多只能有1个library crate
  • 但至少得有1个crate。

创建一个只包含library crate的项目:

1
cargo new project_lib --lib

模块

例:

src/main.rs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
use planb::models_one::structs::HousePrice; //引用路径
//如果想引用同级别下不同模块使用{},引用所以模块使用*


fn main() {
let one = planb::models_one::enums::YesNo::Yes; //绝对路径
//调用上一级模块路径使用super,路径表示为自己使用self
let house_price = HousePrice {
price: 1000,
area: String::from("shanghai"),
bed_rooms: 3,
main_road: planb::models_one::enums::YesNo::Yes
};
}

src/lib.rs:

1
2
pub mod models_one; //在main.rs同级目录下新建文件 模块名.rs
pub mod models_two; //在main.rs同级目录下新建文件夹 模块名/mod.rs

src/models_one.rs:

1
2
pub mod enums; //创建子模块:在main.rs下新建文件夹 主模块名,然后新建文件 主模块名/子模块名.rs
pub mod structs;

src/models_one/enums.rs:

1
2
3
4
pub enum YesNo {
Yes,
No,
}

src/models_one/structs.rs:

1
2
3
4
5
6
pub struct HousePrice {
pub price: u32,
pub area: String,
pub bed_rooms: u32,
pub main_road: crate::models_one::enums::YesNo
}

src/models_two/mod.rs:

1
2
3
fn fun() {

}

学习rust_day05
https://zlsf-zl.github.io/2026/03/14/学习rust-day05/
作者
ZLSF
发布于
2026年3月14日
许可协议