Rust聲明宏在不同K線bar類型中的應(yīng)用小結(jié)
Rust的宏功能博大精深。在K線bar中,往往有很多不同分時k線圖,比如1,2,3,5,。。。。60,120,250,300…。。不同分鐘類型。
如果不用宏,那么手寫會比較麻煩。下面就試用一下宏來實現(xiàn)不同類型的bar.
一、數(shù)據(jù)和功能
bar的結(jié)構(gòu)
為了簡單起見,我們把不同分時的Bar抽象成下面的結(jié)構(gòu)。
struct Bar{ open :f64, close:f64, high:f64, low:f64, }
同時這個結(jié)構(gòu)要實現(xiàn)一個trait
trait BarPrint{ fn print_self(&self); }
二、單一bar的實現(xiàn)
我們先考慮,impl_single_bar接受單一的類型參數(shù),比如Bar1,Bar3,Bar5,…
trait BarPrint{ fn print_self(&self); } // Bar1,Bar2,Bar3,Bar5,Bar10,Bar15,Bar30,Bar45,Bar60,..... macro_rules! impl_single_bar { ($bar:ident) => ( #[derive(Debug)] struct $bar{ open:f64, close:f64, high:f64, low:f64, } impl $bar{ fn new() -> Self{ $bar{ open:0.0, close:0.0, high:0.0, low:0.0, } } } impl BarPrint for $bar { fn print_self(&self){ println!("impl_single_bar =>close:{:?} open: {:?}, high:{:?}, low:{:?}",&self.close,&self.open,&self.high,&self.low); } } ); } fn main(){ impl_single_bar!(Bar1); //這個可以放在main()函數(shù)外,不影響 let bar = Bar1::new(); println!("bar:{:?}",bar); bar.print_self(); impl_single_bar!(Bar2); let bar2 = Bar2::new(); println!("bar:{:?}",bar2); bar2.print_self(); }
輸出:
bar:Bar1 { open: 0.0, close: 0.0, high: 0.0, low: 0.0 }
impl_single_bar =>close:0.0 open: 0.0, high:0.0, low:0.0
bar:Bar2 { open: 0.0, close: 0.0, high: 0.0, low: 0.0 }
impl_single_bar =>close:0.0 open: 0.0, high:0.0, low:0.0
這樣的確方便了一些,但是因為參數(shù)是一個個輸入,需要
impl_single_bar!(Bar1); impl_single_bar!(Bar2);
每一個類型,寫一行函數(shù),還是不太方便。
注意:
1、impl_single_bar!(Bar1),可以放在main()函數(shù)外,不受影響;
2、$bar:ident,也可以是 $bar:tt。tt是分語樹,比ident概念要大。
三、實現(xiàn)多類型參數(shù)輸入
這里就需要用到rust宏的重復(fù)的寫法。這里不特別展開,相關(guān)的資料很多。
1、試寫一下生成多個類型的宏
macro_rules! create_bars{ ($($bar:ident),*) => { $( #[derive(Debug)] struct $bar{ open:f64, close:f64, high:f64, low:f64, } )* } }
2、上面也可以跳過,直接
trait BarPrint{ fn print_self(&self); } macro_rules! impl_multi_bars{ ($($bar:ident),*) => { $( #[derive(Debug)] struct $bar{ open:f64, close:f64, high:f64, low:f64, } impl $bar{ fn new() -> Self{ $bar{ open:0.0, close:0.0, high:0.0, low:0.0, } } } impl BarPrint for $bar { fn print_self(&self){ println!("impl_multi_bars => close:{:?} open: {:?}, high:{:?}, low:{:?}",&self.close,&self.open,&self.high,&self.low); } } )* } } fn main(){ create_bars!(Bar3,Bar4); let bar3 =Bar3{open:0.0,close:0.0,high:0.0,low:0.0}; println!("bar3:{:?}",bar3); let bar4 =Bar4{open:0.0,close:0.0,high:0.0,low:0.0}; println!("bar4:{:?}",bar4); // 測試生成多個struct Bar5,Bar6,Bar7,同時測試其實現(xiàn)的方法 impl_multi_bars!(Bar5,Bar6,Bar7);//可以放在main()函數(shù)外,在main()函數(shù)內(nèi),直接調(diào)用即可。 let bar5 = Bar5::new(); println!("bar5:{:?}",bar5); bar5.print_self(); }
輸出:
bar3:Bar3 { open: 0.0, close: 0.0, high: 0.0, low: 0.0 }
bar4:Bar4 { open: 0.0, close: 0.0, high: 0.0, low: 0.0 }
bar5:Bar5 { open: 0.0, close: 0.0, high: 0.0, low: 0.0 }
impl_multi_bars => close:0.0 open: 0.0, high:0.0, low:0.0
和2相比,你可需要把多個類型寫到一行中就行了,即:
impl_multi_bars!(Bar5,Bar6,Bar7);
到此這篇關(guān)于Rust聲明宏在不同K線bar類型中的應(yīng)用小結(jié)的文章就介紹到這了,更多相關(guān)Rust聲明宏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Rust開發(fā)環(huán)境搭建到運行第一個程序HelloRust的圖文教程
本文主要介紹了Rust開發(fā)環(huán)境搭建到運行第一個程序HelloRust的圖文教程,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12Rust中的Box<T>之堆上的數(shù)據(jù)與遞歸類型詳解
本文介紹了Rust中的Box<T>類型,包括其在堆與棧之間的內(nèi)存分配,性能優(yōu)勢,以及如何利用Box<T>來實現(xiàn)遞歸類型和處理大小未知類型,通過Box<T>,Rust程序員可以更靈活地管理內(nèi)存,避免編譯時大小不確定的問題,并提高代碼的效率和靈活性2025-02-02