rust 多文件编程

rust 多文件编程


main.rs lib.rs mod.rs文件对同一目录下的其他文件的装饰,或者使之成为一个mod

// main.rs or lib.rs mod.rs
pub mod a;// let a.rs be a mod

多文件夹模式

# 文件夹a
-a
 |-b
   |-c
     |-d.rs
     |-mod.rs
   |-mod.rs
 |-mod.rs

#a/b/c/
mod.rs 的形式
pub mod d;

使用

// main.rs
mod a;
use a::b::c::d;// mod.rs is to pub a mod named as a  file

更类似与c语言的优雅但是也有缺点的include方式

// file: include/pure_data_structure/stack.rs
#[derive(Debug)]
pub struct Stack<T>{
    top:usize,
    data:Vec<T>,
}
// file: src/pure_ds_impl/stack.rs
include!("../../include/pure_data_structure/stack.rs");

impl <T> Stack<T> {
    fn new()->Self{
        Stack{
            top:0,
            data:Vec::new()
        }
    }
    fn push(&mut self,val:T){
        self.data.push(val);
        self.top += 1;
    }

    fn pop(&mut self)->Option<T>{
        if self.top == 0{return None;}
        self.top -= 1;
        self.data.pop()
    }

    fn peek(&self)->Option<&T>{
        if self.top == 0{return None;}
        self.data.get(self.top - 1)
    }
    fn is_empty(&self)->bool{
        0 == self.top
    }
    fn size(&self)->usize{
        self.top
    }
}
// test_suits case
// file: test_suits/test.rs
include!("../src/pure_ds_impl/stack.rs");

// test:stack.rs
fn test_stack(){
    let mut s = Stack::new();
    s.push(1);
    println!("{:?}",s);
    s.pop();
    println!("{:?}",s);
    s.push(1);
    s.push(1);
    s.push(1);
    s.push(1);
    println!("{:?}",s.peek().unwrap());
    println!("{}",s.size());
    println!("{}",s.is_empty());
}
// end stack.rs
// file:src/main.rs
include!("../test_suits/test.rs");
fn  use_test_case_v1(){
   #[cfg(any(windows))]// condictional run
   test_stack();
   // #[cfg(all(windows))]
   test_stack();
   // #[cfg(all(windows,unix))]
   let a = par_check_v1();
   println!("{}", a);
}
fn main() {
   use_test_case_v1();
}

你可能感兴趣的:(经验管理,rust,开发语言,后端)