切片
例:
1 2 3 4 5 6
| fn main() { let s = String::from("Hello World");
let hello: &str = &s[0..5]; let world: &str = &s[6..11]; }
|
结构体
例:
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); let none = fun(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), _ => { 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; 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; pub mod models_two;
|
src/models_one.rs:
1 2
| pub mod enums; 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: