gw_rust_programming_tutorial/
lib.rs

1//在 mod front_of_house 后使用分号,而不是代码块,这将告诉 Rust 在另一个与模块同名的文件中加载模块的内容
2mod front_of_house;
3
4pub use crate::front_of_house::hosting;
5
6pub fn eat_at_restaurant() {
7    hosting::add_to_waitlist();
8}
9
10pub mod chapter_8;
11pub use crate::chapter_8::test_collection;
12pub use crate::chapter_8::test_string;
13pub use crate::chapter_8::test_map;
14
15pub fn test_chapter_8()
16{
17    test_collection::test_vec();
18    test_string::test_string();
19    test_map::test_hash_map();
20}
21
22//9
23pub mod chapter_9;
24pub use crate::chapter_9::test_panic;
25pub fn test_chapter_9()
26{
27    test_panic::test_painic_fn();
28    //test_panic::test_panic_resume();
29    test_panic::read_username_from_file();
30}
31
32//10
33pub mod chapter_10;
34pub use crate::chapter_10::test_generics;
35pub use crate::chapter_10::test_traits;
36pub use crate::chapter_10::test_lifetime;
37pub fn test_chapter_10()
38{
39    println!("=========================test chapter 10 ====================");
40    let v=vec![1,5,8,1,2];
41    let maxv = test_generics::test_generics_fn(&v);
42    println!("max={}",maxv);
43    let maxv = test_generics::test_generics_fn1(&v);
44    println!("max={}",maxv);
45
46    test_traits::test_trait_fn();
47
48    test_lifetime::test_lifetime_fn();
49}
50
51//12
52pub mod chapter_12;
53pub use crate::chapter_12::cmdargs;
54pub use crate::chapter_12::testfile;
55pub use crate::chapter_12::test_12_4;
56pub use crate::chapter_12::test_12_6;
57
58pub fn test_chapter_12()
59{
60    cmdargs::test_getargs();
61    testfile::test_file();
62    test_12_4::test_test_driver_dev();
63    test_12_6::test_err_to_stderr();
64}
65
66//13
67pub mod chapter_13;
68pub use crate::chapter_13::test_closure;
69use crate::chapter_13::file_iterator;
70
71pub fn test_chapter_13()
72{
73    test_closure::fn_closure();
74    file_iterator::test_13_2();
75}
76
77// mod front_of_house {
78//     pub mod hosting {
79//         pub fn add_to_waitlist() {
80//             println!("add_to_waitlist");
81//         }
82
83//         fn seat_at_table() {}
84//     }
85
86//     mod serving {
87//         fn take_order() {}
88
89//         fn serve_order() {}
90
91//         fn take_payment() {}
92//     }
93// }
94
95// pub fn eat_at_restaurant1() {
96//     // 绝对路径
97//     crate::front_of_house::hosting::add_to_waitlist();
98
99//     // 相对路径
100//     front_of_house::hosting::add_to_waitlist();
101// }
102
103// //创建公有结构体和枚举
104// mod back_of_house {
105//     pub struct Breakfast {
106//         pub toast: String,
107//         seasonal_fruit: String,
108//     }
109
110//     impl Breakfast {
111//         pub fn summer(toast: &str) -> Breakfast {
112//             Breakfast {
113//                 toast: String::from(toast),
114//                 seasonal_fruit: String::from("peaches"),
115//             }
116//         }
117//     }
118// }
119
120// pub fn eat_at_restaurant() {
121//     // 在夏天点一份黑麦面包作为早餐
122//     let mut meal = back_of_house::Breakfast::summer("Rye");
123//     // 更改我们想要的面包
124//     meal.toast = String::from("Wheat");
125//     println!("I'd like {} toast please", meal.toast);
126
127//     // 如果取消下一行的注释,将会导致编译失败;我们不被允许
128//     // 看到或更改随餐搭配的季节水果
129//     // meal.seasonal_fruit = String::from("blueberries");
130// }