Rust4.1 Managing Growing Projects with Packages, Crates, and Modules

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

// 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

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

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

pub fn add_to_waitlist(){}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值