rust05模塊

庫(crate)

Rust中有四種庫(crate):core crate(核心庫)、std crate(標準庫)、binary crate(二進制庫)、extern crate(外部庫),其中核心庫的代碼提供了不少特殊的方法讓咱們編寫代碼時無需顯式導入就能夠直接使用例如: println!、Some、enum、str、fn、impl等; 標準庫和外部庫代碼是須要自行導入才能使用,它們兩的區別是標準庫由語言自己自帶(由rust官方團隊來維護),而外部庫庫是一個外部公共庫(由開發者們來本身維護).vim

Rust中建立一個模塊須要使用Cargo來完成;與以前利用Cargo來建立一個二進制項目代碼(cargo new --bin PROJECT_NAME)所需提供的參數不一樣; Cargo默認狀況下建立的並非二進制項目代碼,而是外部庫(cargo new CRATE_NAME)。 函數

 
 

建立一個外部庫

# 建立一個二進制庫
[zhengtong@localhost ~]$ cargo new --bin learn_rust
[zhengtong@localhost ~]$ cd learn_rust

# 建立一個外部庫
[zhengtong@localhost learn_rust]$ cargo new communicator

# 目錄結構
[zhengtong@localhost learn_rust]$ tree ../learn_rust
../learn_rust/
├── Cargo.toml
├── communicator
│   ├── Cargo.toml
│   └── src
│       └── lib.rs
├── src
│   └── main.rs

# 查看樣例代碼
[zhengtong@localhost learn_rust]$ cat communicator/src/lib.rs 
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
    }
}

上面這個代碼片斷建立了一個二進制庫(learn_rust)和一個外部庫(communicator)。 debug

binary crate:src/main.rs
extern crate:src/lib.rscode

rust只容許binary crate 代碼入口在src/main.rs中,
rust只容許extern crate 代碼入口在src/lib.rs中. server

 
 

定義一個模塊

備註:Rust容許函數塊中不填寫任何代碼。 ci

[zhengtong@localhost learn_rust]$ vim communicator/src/lib.rs 
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
    }
}

mod network {   // 模塊: 不須要圓括號

    mod server {
        fn start() {
        }

        fn receive() {
        }

        fn send() {
        }
    }

    mod client {
        fn connect() {
        }

        fn send() {
        }

        fn receive() {
        }
    }

}

 
 

公開模塊

Rust中全部模塊默認都是私有的,須要指定pub以後才能被外部調用.開發

[zhengtong@localhost learn_rust]$ vim communicator/src/lib.rs 
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
    }
}

pub mod network {           // 公開模塊

    pub mod server {        // 公開模塊

        pub fn start() {    // 公開函數
            println!("start the server!")
        }

        pub fn receive() {  // 公開函數
            println!("receive data")
        }

        pub fn send() {     // 公開函數
            println!("send data back to client")
        }

    }

    pub mod client {

        pub fn connect() {
            println!("connect to server")
        }

        pub fn send() {
            println!("send data to server")
        }

        pub fn receive() {
            println!("receive data from server")
        }
    }

}

在learn_rust二進制庫中引用communicator外部庫. get

# 編輯Cargo.toml配置文件
[zhengtong@localhost learn_rust]$ vim Cargo.toml 
[package]
name = "learn_rust"
version = "0.1.0"
authors = ["zhengtong"]

[dependencies]
communicator = { path = "./communicator" }  //添加這行



# 引用communicator模塊
[zhengtong@localhost learn_rust]$ vim src/main.rs

extern crate communicator;  // 引入外部庫


fn main() {
    communicator::network::server::start();  // 調用外部庫的server模塊的start函數.
    println!("hello world!")
}

運行結果it

cargo run
   Compiling communicator v0.1.0 (file:///Users/zhengtong/PycharmProjects/learn_rust/communicator)
   Compiling learn_rust v0.1.0 (file:///Users/zhengtong/PycharmProjects/learn_rust)
    Finished dev [unoptimized + debuginfo] target(s) in 0.47 secs
     Running `target/debug/learn_rust`
start the server!
hello world!

 
 

將模塊代碼拆分到另一個文件中

communicator/src/lib.rs
[zhengtong@localhost learn_rust]$ vim communicator/src/lib.rs

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
    }
}

pub mod network;    // 這裏發生變化
communicator/src/network.rs
[zhengtong@localhost learn_rust]$ vim communicator/src/network.rs

pub mod server {    

    pub fn start() {
        println!("start the server!")
    }

    pub fn receive() {
        println!("receive data")
    }

    pub fn send() {
        println!("send data back to client")
    }

}

pub mod client {

    pub fn connect() {
        println!("connect to server")
    }

    pub fn send() {
        println!("send data to server")
    }

    pub fn receive() {
        println!("receive data from server")
    }
}

 
 

深度拆分模塊代碼

communicator/src/lib.rs
[zhengtong@localhost learn_rust]$ vim communicator/src/lib.rs

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
    }
}

pub mod network;    // 這裏發生變化
communicator/src/network/mod.rs
pub mod server;     // 這裏發生變化

pub mod client;     // 這裏發生變化
communicator/src/network/server.rs
pub fn start() {
    println!("start the server!")
}

pub fn receive() {
    println!("receive data")
}

pub fn send() {
    println!("send data back to client")
}
communicator/src/network/client.rs
pub fn connect() {
    println!("connect to server")
}

pub fn send() {
    println!("send data to server")
}

pub fn receive() {
    println!("receive data from server")
}
相關文章
相關標籤/搜索