BruceChen7 / gitblog

My blog
6 stars 1 forks source link

rust之模块化 #50

Open BruceChen7 opened 1 year ago

BruceChen7 commented 1 year ago

参考资料

cargo 命令

自定义 cargo build

描述项目信息

[package]
name = "guessing_game"
version = "0.1.0"
edition = "2018"
description = "A fun game where you guess what number the computer has chosen."
license = "MIT OR Apache-2.0"

[dependencies]

cargo feature

[package]
name = "foo"

[features]
derive = ["syn"]

[dependencies]
syn = { version = "1", optional = true }

默认 feature

[features]
default = ["ico", "webp"] // 默认的两个feature
bmp = []
png = []
ico = ["bmp", "png"]
webp = []

条件依赖

[dependencies]
gif = { version = "0.11.1", optional = true }

// rust code
cfg(feature = "gif")
[dependencies]
ravif = { version = "0.6.3", optional = true }
rgb = { version = "0.8.25", optional = true }

[features]
avif = ["ravif", "rgb"]

project, package, crate, module 的区别

module tree

项目结构

代码组织

crates can be mappe to a

workspace

例子

在不同文件下访问 mod

// ↳ main.rs
mod greetings; // import greetings module

fn main() {
  greetings::hello();
}

// ↳ greetings.rs
// ⭐️ no need to wrap the code with a mod declaration. File itself acts as a module.
pub fn hello() { // function has to be public to access from outside
  println!("Hello, world!");
}

// 如果在 mod 下嵌套的其他的 mod,那么该 mod 必须声明为 pub
// ↳ main.rs
mod phrases;

fn main() {
  phrases::greetings::hello();
}

// ↳ phrases.rs
// mod 嵌套
pub mod greetings { // ⭐️ module has to be public to access from outside
  pub fn hello() {
    println!("Hello, world!");
  }
}

在不同目录中模块

// ↳ main.rs
mod greetings;

fn main() {
  // 注意名字的写法
  greetings::hello();
}

// ↳ greetings/mod.rs
pub fn hello() { // ⭐️ function has to be public to access from outside
  println!("Hello, world!");
}
// ↳ main.rs
mod phrases;

fn main() {
   // 在 mod 中声明了新的 mod,那么这部分也成为名字空间的一部分
  phrases::greetings::hello();
}

// ↳ phrases/mod.rs
pub mod greetings { // ⭐️ module has to be public to access from outside
  pub fn hello() {
    println!("Hello, world!");
  }
}

在 mod.rs 中访问子 mod

/ ↳ main.rs
mod phrases;

fn main() {
  phrases::hello()
}

// ↳ phrases/mod.rs
mod greetings;

pub fn hello() {
  greetings::hello()
}

// ↳ phrases/greetings.rs
pub fn hello() {
  println!("Hello, world!");
}

crates

lib.rs 和 main.rs 在一块使用

在 cargo.toml 声明的依赖 crates

crate metadata

profile section

发布 crate 到 crates.io 中,文档的编写

// src/lib.rs
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
    x + 1
}
 Doc-tests my_crate

running 1 test
test src/lib.rs - add_one (line 5) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.27s

contained items文档编写

//! # My Crate
//!
//! `my_crate` is a collection of utilities to make performing certain
//! calculations more convenient.

/// Adds one to the number given.
// --snip--
///
/// # Examples
///
/// ```
/// let arg = 5;
/// let answer = my_crate::add_one(arg);
///
/// assert_eq!(6, answer);
/// ```
pub fn add_one(x: i32) -> i32 {
    x + 1
}

workspace

// # Think we run
mkdir greetings
touch greetings/Cargo.toml
cargo new greetings/lib
cargo new --bin greetings/examples/hello

// # That generates,
.
├── Cargo.toml
├── examples
│  └── hello
│     ├── Cargo.toml
│     └── src
│        └── main.rs
└── lib
   ├── Cargo.toml
   └── src
      └── lib.rs

// # Think we modify following files

// 01. greetings/Cargo.toml
[workspace]
members = [
    "lib",
    "examples/hello"
]

// 02.1 greetings/lib/Cargo.toml
[package]
name = "greetings"
version = "0.1.0"
authors = ["Dumindu Madunuwan"]

[dependencies]

// 02.2 greetings/lib/src/lib.rs
pub fn hello() {
    println!("Hello, world!");
}

// 03.1 greetings/examples/hello/Cargo.toml
[package]
name = "hello"
version = "0.1.0"
authors = ["Dumindu Madunuwan"]

[dependencies]
// 在 crates 中学会依赖
greetings = { path = "../../lib" }

// 03.2 greetings/examples/hello/src/main.rs
extern crate greetings;

fn main() {
    greetings::hello();
}

use

// -- 01. importing elements --
use std::fs::File;

fn main() {
    File::create("empty.txt").expect("Can not create the file!");
}

// -- 02. importing module and elements--
std::fs::{self, File} //use std::fs; use std::fs::File;

fn main() {
    fs::create_dir("some_dir").expect("Can not create the directry!");
    File::create("some_dir/empty.txt").expect("Can not create the file!");
}

// -- 03. importing multiple elements
use std::fs::File;
use std::io::{BufReader, BufRead}; //use std::io::BufReader; use std::io::BufRead;

fn main() {
    let file = File::open("src/hello.txt").expect("file not found");
    let buf_reader = BufReader::new(file);

    for line in buf_reader.lines() {
        println!("{}", line.unwrap());
    }
}

对于 std crates,不需要使用extern crate std;

pub use

// ↳ main.rs
mod phrases;

fn main() {
    phrases::hello(); //not directly map
}

// ↳ phrases/mod.rs
pub mod greetings;

pub use self::greetings::hello; //re-export greetings::hello to phrases

// ↳ phrases/greetings.rs
pub fn hello() {
  println!("Hello, world!");
}

注释

// Line comments
/* Block comments */

type/rust #public