Rust学习笔记

Rust编程语言入门教程课程笔记

参考教材: The Rust Programming Language (by Steve Klabnik and Carol Nichols, with contributions from the Rust Community)

Lecture 7: Managing Growing Projects with Packages, Crates, and Modules

src/main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// src/main.rs: the main file of the project, where the main function is defined; this is the crate root
// src/lib.rs: the root of your crate’s library; the library’s name is the same as the name of the crate
// src/bin: directory that can contain multiple binary crates; each file in this directory will be a separate binary crate
// src/bin/main.rs: the main file of the binary crate with the same name as the directory; this file is the crate root of the binary crate

//Modules
use std::collections::HashMap;// use keyword to bring module into scope

use std::io::Result as IoResult;// use keyword to bring module into scope
use std::{cmp, io};// use keyword to bring module into scope
use rand::Rng;// use keyword to bring module into scope
use std::collections::*;// use keyword to bring module into scope, * is glob operator

fn main() {
let mut map = HashMap::new();
map.insert(1, 2);
println!("{:?}", map);
let m = IoResult::Ok(());
println!("{:?}", m);

let mut rng = rand::thread_rng();
let t = rng.gen_range(1..=10);
println!("{:?}", t);
}

src/lib.rs

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
mod front_of_house;//{// module

// pub mod hosting{// public module
// pub fn add_to_waitlist(){}
// }

// mod serving{// private module
// fn take_order(){}
// fn serve_order(){}
// fn take_payment(){}
// }

// fn fix_incorrect_order(){
// cook_order();
// super::serve_order();// super keyword to access parent module
// crate::serve_order();// absolute path
// }

// pub fn cook_order(){}

// pub struct Breakfast{
// pub toast: String,
// seasonal_fruit: String,
// }

// impl Breakfast{
// pub fn summer(toast: &str) -> Breakfast{
// Breakfast{
// toast: String::from(toast),
// seasonal_fruit: String::from("peaches"),
// }
// }
// }
//}

pub use crate::front_of_house::hosting;// use keyword to bring module into scope
//use crate::front_of_house::servering;// cannot use private module
//use front_of_house::hosting;// relative path

pub fn eat_at_restaurant(){
// Absolute path
crate::front_of_house::hosting::add_to_waitlist();

// Relative path
front_of_house::hosting::add_to_waitlist();

// Order a breakfast in the summer with Rye toast
let mut meal = front_of_house::Breakfast::summer("Rye");
// Change our mind about what bread we'd like
meal.toast = String::from("Wheat");
println!("I'd like {} toast please", meal.toast);
// The next line won't compile if we uncomment it; we're not allowed
// to see or modify the seasonal fruit that comes with the meal
// meal.seasonal_fruit = String::from("blueberries");

hosting::add_to_waitlist();

}

fn serve_order(){}

src/front_of_house.rs

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
pub mod hosting; //{// public module
// pub fn add_to_waitlist(){}
// }

mod serving{// private module
fn take_order(){}
fn serve_order(){}
fn take_payment(){}
}

fn fix_incorrect_order(){
cook_order();
super::serve_order();// super keyword to access parent module
crate::serve_order();// absolute path
}

pub fn cook_order(){}

pub struct Breakfast{
pub toast: String,
seasonal_fruit: String,
}

impl Breakfast{
pub fn summer(toast: &str) -> Breakfast{
Breakfast{
toast: String::from(toast),
seasonal_fruit: String::from("peaches"),
}
}
}

src/front_of_house/hosting.rs

1
pub fn add_to_waitlist(){}