Rust语言学习笔记(9)

Packages and Crates(包和箱)

  • 一个包包含一个或多个箱。
    箱相当于工程,包就是相关工程的集合。
  • 包内包含一个 Cargo.toml 文件
  • 箱可分为二进制箱(binary crate)和库箱(library crate)。
  • 一个包只能包含零个或一个库箱。
    库箱的位置在 src/lib.rs(根模块)中。
  • 一个包可以包含任意多个二进制箱。
    二进制箱的位置在 src/main.rs(根模块) 以及 src/bin 目录下的其他文件中。
  • 一个包必须至少包含一个箱。
$ cargo new my-project
     Created binary (application) `my-project` package
$ ls my-project
Cargo.toml
src
$ ls my-project/src
main.rs

创建模块

创建“餐厅”工程

$ cargo new --lib restaurant
// src/lib.rs
// 前台模块
mod front_of_house {
    mod hosting {
        fn add_to_waitlist() {}
        fn seat_at_table() {}
    }
    mod serving {
        fn take_order() {}
        fn serve_order() {}
        fn take_payment() {}
    }
}

库的树结构

crate
 └── front_of_house
     ├── hosting
     │   ├── add_to_waitlist
     │   └── seat_at_table
     └── serving
         ├── take_order
         ├── serve_order
         └── take_payment

路径与可见性

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}
pub fn eat_at_restaurant() {
    // Absolute path
    crate::front_of_house::hosting::add_to_waitlist();
    // Relative path
    front_of_house::hosting::add_to_waitlist();
}
fn serve_order() {}
mod back_of_house {
    fn fix_incorrect_order() {
        cook_order();
        super::serve_order();
    }
    fn cook_order() {}
}
mod back_of_house {
    pub enum Appetizer {
        Soup,
        Salad,
    }
}
pub fn eat_at_restaurant() {
    let order1 = back_of_house::Appetizer::Soup;
    let order2 = back_of_house::Appetizer::Salad;
}
  • 模块,模块的成员,结构体的成员缺省为 private
  • 枚举的成员缺省为 public
  • 父模块的成员只能访问子模块的 public 成员
  • 子模块的成员只能访问父模块的 private 成员

use

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}
use crate::front_of_house::hosting; // 绝对路径
// use front_of_house::hosting; // 相对路径
pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
}
mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}
use crate::front_of_house::hosting::add_to_waitlist;
pub fn eat_at_restaurant() {
    add_to_waitlist();
    add_to_waitlist();
    add_to_waitlist();
}
use std::collections::HashMap;
fn main() {
    let mut map = HashMap::new();
    map.insert(1, 2);
}

as

use std::fmt;
use std::io;
fn function1() -> fmt::Result {
    // --snip--
}
fn function2() -> io::Result<()> {
    // --snip--
}
use std::fmt::Result;
use std::io::Result as IoResult;
fn function1() -> Result {
    // --snip--
}
fn function2() -> IoResult<()> {
    // --snip--
}

pub use

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
}

外部包

Cargo.toml

[dependencies]
rand = "0.5.5"
use rand::Rng;
fn main() {
    let secret_number = rand::thread_rng().gen_range(1, 101);
}

嵌套路径

use std::io;
use std::cmp::Ordering;
// ---snip---
// 相当于
use std::{cmp::Ordering, io};
// ---snip---
use std::io;
use std::io::Write;
// 相当于
use std::io::{self, Write};

Glob 操作符

use std::collections::*;

模块与文件

// src/lib.rs
mod front_of_house;
pub use crate::front_of_house::hosting;
pub fn eat_at_restaurant() {
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
    hosting::add_to_waitlist();
}
// src/front_of_house.rs
pub mod hosting {
    pub fn add_to_waitlist() {}
}
原文地址:https://www.cnblogs.com/zwvista/p/12736344.html