一、mod.rs和lib.rs
mod.rs往往是把同一目錄下的n個rs文件綜合在一起的有效方式;
lib.rs是一個庫或子庫層次綜合在一起的有效方式;
下面舉個實例來說明。生成一個rusttoc本地庫(由cargo new rusttoc --lib所生成),目錄結(jié)構(gòu)如下:
songroom@staff-NB-146:~/myffi/rusttoc/src$ ls -R
.:
bar lib.rs
./bar:
data.rs ffi.rs mod.rs
其中,data.rs文件內(nèi)容如下:
#[derive(Debug)]
pub struct Tick;
pub trait GetBar{
fn get_bar(&self)->Vec<Bar>;
}
#[derive(Debug)]
pub struct Bar{
open:f64,
close:f64,
min:f64,
max:f64,
}
impl Bar{
pub fn new() ->Self{
Bar { open: 0.0, close: 0.0, min: 0.0, max: 0.0 }
}
}
其中,ffi.rs文件內(nèi)容如下:
use std::os::raw::{c_int,c_double};
extern "C"{
fn abs(num:c_int) ->c_int;
fn sqrt(x:c_double) ->c_double;
}
#[no_mangle]
pub extern "C" fn rfn_for_c(){
println!("call from rust fn abs :{}",unsafe{abs(-6)});
println!("call from rust fn sqrt :{}",unsafe{sqrt(36.0)});
}
此外,在上面rusttoc庫中,在src目錄下,除了有l(wèi)ib.rs文件,還有一個bar文件夾,在這個文件夾下,還有3個.rs文件,其中包括一個mod.rs文件。
很顯然,mod.rs是把data.rs和ffi.rs綜合在一起對外開放的文件。
而lib.rs是在rusttoc這個層次,綜合起來的一個對外開放的文件。
當然,如果一個庫中,包含若干個子庫,那也會存在多個不同層次的lib.rs的文件。
二、use來管理庫內(nèi)容的對外開放
在每層mod.rs和lib.rs文件中,要通過use來約束對外開放的程度和范圍。
mod.rs文件
pub mod ffi;
pub mod data;
pub use ffi::rfn_for_c;
pub use data::{Bar,GetBar};//這里設置Tick不暴露
lib.rs文件
pub mod bar;
pub use bar::*;
在mod.rs中,通過pub use來約束ffi.rs和data.rs在此庫對外開放(或暴露)的范圍,在這里為rfn_for_c函數(shù),以及Bar,以及GetBar。而沒有進來的Tick,則表明不對外開放(暴露)。
在lib.rs層次,再次明確對外開放的層次,使用use表統(tǒng)一表明,各個mod.rs或子lib.rs的統(tǒng)一對外開放。
如果沒有use安排,以上均會處于關閉狀態(tài),均無法以本地庫或外地庫的方式來調(diào)用。當然,在use前,還要聲明一下各mod。
三、另外一種方式:include
除了mod.rs,lib.rs之外,還有一種特別的導入形式,即include。它可以把不同形式文件的內(nèi)容導入到當?shù)降墓ぷ骺臻g。
下面,在test文件夾下,放了兩個不同后綴的文件。
songroom@staff-NB-146:~/myffi/test$ ls -R
.:
bind.rs const.txt
bind.rs
#[derive(Debug)]
struct Student{
name : String,
age:i32,
}
const.txt
const MIN_VALUE:f64 =30000.0_f64;
四、調(diào)用庫和include導入
另一個rust 二進制工程文件中,有main.rs如下:
include!("../../test/bind.rs");
include!("../../test/const.txt");
pub use rusttoc::*;
struct Strategy;
//實現(xiàn)本地庫中的trait
impl GetBar for Strategy{
fn get_bar(&self)->Vec<Bar>{
vec![Bar::new()]
}
}
fn main() {
let student = Student{name: "wowotuo".to_string(),age:18};
//let tick = Tick;
let bar = Bar::new();
let strategy = Strategy;
println!("bar:{:?}",bar);
println!("strategy ->get_bar:{:?}",strategy.get_bar());//調(diào)用trait
//let tick = Tick; //因為mod.rs中沒有開放Tick的訪問權(quán)限,故不能訪問
//println!("tick:{:?}",tick);
println!("student :{:?}",student);
rfn_for_c();
println!("const :{:?}",MIN_VALUE);
println!("Hello, world!");
}
另外,在這個工程下,cargo.toml文件應加下如下部分,表明需要導入本地庫,并指明相應的路徑:
[dependencies.rusttoc]
path = "../rusttoc" #庫的位置
這樣,就是main.rs中導入了rusttoc庫,以及include了兩個文件進來了。文章來源:http://www.zghlxwxcb.cn/news/detail-730529.html
songroom@staff-NB-146:~/myffi/work/src$ cargo run --release
Compiling work v0.1.0 (/home/songroom/myffi/work)
Finished release [optimized] target(s) in 1.68s
Running `/home/songroom/myffi/work/target/release/work`
bar:Bar { open: 0.0, close: 0.0, min: 0.0, max: 0.0 }
strategy ->get_bar:[Bar { open: 0.0, close: 0.0, min: 0.0, max: 0.0 }]
student :Student { name: "wowotuo", age: 18 }
call from rust fn abs :6
call from rust fn sqrt :6
const :30000.0
Hello, world!
上面分別實現(xiàn)本地庫中trait,調(diào)用了const,相關函數(shù)和struct等。文章來源地址http://www.zghlxwxcb.cn/news/detail-730529.html
到了這里,關于Rust :mod.rs和lib.rs中use的作用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!