```rust fn main() { use doe::traits::DebugPrint; use doe::bfn; use doe::Bfn; #[derive(Debug)] struct Demo<'a>{ name:&'a str } let f0 = bfn!(||{println!("ok");}); f0.call(); fn func()->Demo<'static>{ let d = Demo{name: "andrew"}; d } let f1 = bfn!(func); f1.call().println();
fn sum()->usize{
9+89
}
let f2 = Bfn::new(Box::new(sum));//or bfn!(sum);
f2.call().println();
} ```
rust
fn main() {
use doe::traits::DebugPrint;
use doe::Bts;
use doe::bts;
let mut s:Bts = bts!("Trait Object, it's ");
s.push(bts!(100));
s.push_str("% safe");
s.println();//"Trait Object, it's 100% safe"
s.as_bytes().println();//[84, 114, 97, 105, 116, 32, 79, 98, 106, 101, 99, 116, 44, 32, 105, 116, 39, 115, 32, 49, 48, 48, 37, 32, 115, 97, 102, 101]
s.chars().println();//['T', 'r', 'a', 'i', 't', ' ', 'O', 'b', 'j', 'e', 'c', 't', ',', ' ', 'i', 't', '\'', 's', ' ', '1', '0', '0', '%', ' ', 's', 'a', 'f', 'e']
let b = Into::<Bts>::into("b".to_string());
let b = Bts::from("demo");
}
rust
fn main() {
use std::sync::{Arc, Mutex};
use doe::impl_display;
use doe::impl_debug;
struct Doe{
pub name:String,
pub nickname: Box<str>,
key:Arc<Mutex<usize>>
}
impl_display!(Doe,name,nickname,key);
impl_debug!(Doe,name,nickname,key);
let d = Doe{name:"andrew".to_string(), nickname: Box::from("func"),key:Arc::new(Mutex::new(15))};
println!("{:?}",d);
println!("{}",d);
}
rust
fn main() {
use doe::traits::Print;
use doe::traits::Str;
use doe::targets;
"this is a {s},I like Trait Object {p}%"
.format(vec![(Box::new("{s}"),Box::new("demo")),(Box::new("{p}"),Box::new(100))]).println();//this is a demo,I like Trait Object 100%
"this is a {d},I like Trait Object {p}}%"
.format(targets!{"{d}"=>"demo","{p}"=>100})
.println(); //this is a demo,I like Trait Object 100%
}
```rust use doe::traits::Print; use doe::traits::DebugPrint; "printable text".print();//impl Display "printable text".println(); "printable text".eprintln();
struct DebugPrintDemo{}//impl Debug let d = DebugPrintDemo{}; d.print(); d.println(); d.eprintln(); ```
rust
fn main() {
use doe::targets;
use doe::traits::Print;
use doe::traits::Str;
"this is a {d},I like Trait Object {p}}%"
.format(targets!{"{d}"=>"demo","{p}"=>100})
.println(); //this is a demo,I like Trait Object 100%
}
```rust fn main() { use std::sync::{Arc, Mutex}; let run_state = Arc::new(Mutex::new(true));
let t1 = std::thread::spawn({
let run_state = Arc::clone(&run_state);
move || {
std::thread::sleep(std::time::Duration::from_secs(5));
*run_state.lock().unwrap() = false;
}
});
let t2 = std::thread::spawn({
let run_state = Arc::clone(&run_state);
move || {
run_timer_by_state(
std::time::Duration::from_secs(1),
run_state,
Box::new(|| {
// Function body goes here
println!("Running...");
}),
);
}
});
// Wait for the threads to finish
t1.join().expect("Thread panicked");
t2.join().expect("Thread panicked");
} ```
rust
fn run(){
println!("running ");
}
run_timer(std::time::Instant::now(),std::time::Duration::from_secs(1),std::time::Duration::from_secs(5),Box::new(run));
```rust fn main() { use doe::fs::*; use doe::traits::DebugPrint; use doe::traits::Str; use std::ops::Deref; // append data to file appenddatatofile("demo.txt", "demo".asbytes().to_vec()).unwrap();
// get all files and folders
walk_dir(".".to_path().deref()).unwrap().println();
// get all folders
walk_dir_get_files(".".to_path().deref()).unwrap().println();
// get all folders
walk_dir_get_folders(".".to_path().deref())
.unwrap()
.println();
//move file the directory to a new directory
move_file(
"/Users/ryanandrew/code/test/t1/demo.zip".to_path().deref(),
"/Users/ryanandrew/code/test/t2/d2.zip".to_path().deref(),
)
.unwrap();
//copy file the directory to a new directory
copy_file(
"/Users/ryanandrew/code/test/t1/demo.zip".to_path().deref(),
"/Users/ryanandrew/code/test/t2/d2.zip".to_path().deref(),
)
.unwrap();
//move all files in the directory to a new directory
move_folder(
"/Users/ryanandrew/code/test/t1".to_path().deref(),
"/Users/ryanandrew/code/test/t2/t1".to_path().deref(),
)
.unwrap();
//copy all files in the directory to a new directory
copy_folder(
"/Users/ryanandrew/code/test/d1".to_path().deref(),
"/Users/ryanandrew/code/test/d2".to_path().deref(),
)
.unwrap();
} ```
rust
pub fn run() {
system("ping bing.com");
system("ls -a");
// in windows
system_powershell!("ls");
system_cmd!("python -m http.server");
}
rust
use doe::traits::Str;
let a = "this is a demo {}";
let c = a.cut(-1,-2,1);
let f = a.format("so {}").format("how good is");
println!("{:?}",f);
println!("{:?}",c);
rust
use doe::*;
let s = as_to!(5., i64);
assert_eq!(5, s);
rust
use doe::*;
assert_eq!(macros::is_i8(&5_i8),true);
rust
use doe::*;
assert_eq!(macros::is_i16(&5_i16),true);
rust
use doe::*;
assert_eq!(macros::is_i32(&5_i32),true);
rust
use doe::*;
assert_eq!(macros::is_i64(&5_i64),true);
rust
use doe::*;
assert_eq!(macros::is_i128(&5_i128),true);
rust
use doe::*;
assert_eq!(macros::is_f32(&5.0_f32),true);
rust
use doe::*;
assert_eq!(macros::is_f64(&5.0_f64),true);
rust
use doe::*;
let p = powf!(2.,2.);
assert_eq!(p,4.0);
rust
use doe::*;
//cargo run -- -n 100
let arg = args!();
assert_eq!(arg,vec![format!("-n"),format!("100")]);
rust
use doe::*;
let s = input!();
println!("{:?}",s);
rust
use doe::*;
let s = split_to_vec!("aa.bb",".");
assert_eq!(s,vec![format!("aa"),format!("bb")]);
rust
use doe::*;
let s = read_csv!("./data.csv");
assert_eq!(s,vec![vec![format!("a"), format!("b"), format!("c")],vec![format!("1"), format!("2"), format!("3")],vec![format!("10"), format!("20"), format!("30")]]);
rust
use doe::*;
let s1 = sorted!(vec![1.2, 2.6, 0.2]);
let s2 = sorted!(vec![8, 1_i128, 5_i128]);
assert_eq!(s1,vec![0.2,1.2,2.6]);
assert_eq!(s2,vec![1,5,8]);
rust
use doe::*;
let s1 = deduped_sorted!(vec![1.2, 1.2,2.6, 0.2]);
let s2 = deduped_sorted!(vec![8, 1_i128,8,5_i128]);
assert_eq!(s1,vec![0.2,1.2,2.6]);
assert_eq!(s2,vec![1,5,8]);
rust
use doe::*;
let v1: Vec<f64> = vec_element_parse!(vec!["15.", "2.9"], f64);
let v2: Vec<i128> = vec_element_parse!(vec!["15", "2"], i128);
let v3: Vec<f32> = vec_element_parse!(vec![".15", ".2"], f32);
assert_eq!(vec![15.0, 2.9], v1);
assert_eq!(vec![15, 2], v2);
assert_eq!(vec![0.15, 0.2], v3);
rust
use doe::*;
let v1 = vec_element_to_string!(vec!["15.", "2.9"]);
let v2 = vec_element_to_string!(vec![15, 2]);
let v3 = vec_element_to_string!(vec![0.15, 0.2]);
assert_eq!(vec!["15.", "2.9"], v1);
assert_eq!(vec!["15", "2"], v2);
assert_eq!(vec!["0.15", "0.2"], v3);
rust
use doe::*;
let v1 = snail_sort!(vec![vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]]);
let v2 = snail_sort!(vec![vec![1.1, 2.1, 3.1],vec![4.1, 5.1, 6.1],vec![7.1, 8.1, 9.1]]);
assert_eq!(vec![1, 2, 3, 6, 9, 8, 7, 4, 5], v1);
assert_eq!(vec![1.1, 2.1, 3.1, 6.1, 9.1, 8.1, 7.1, 4.1, 5.1], v2);
rust
use doe::*;
let m1 = [[1, 2, -1], [-1, 3, 4], [1, 1, 1]].map(|s| s.to_vec()).to_vec();
let m2 = [[5, 6], [-5, -6], [6, 0]].map(|s| s.to_vec()).to_vec();
let mul_result1 = multiply_matrix!(&m1, &m2);
assert_eq!(mul_result1, [[-11, -6], [4, -24], [6, 0]]);
let m11 = [[1., 2., -1.], [-1., 3., 4.]].map(|s| s.to_vec()).to_vec();
let m22 = [[5.5, 6.], [-5., -6.5]].map(|s| s.to_vec()).to_vec();
let mul_result2 = multiply_matrix!(&m11, &m22);
assert_eq!(mul_result2, [[-4.5, -7.0], [-20.5, -25.5]]);
let m111 = [[1., 2., -1.], [-1., 3., 4.]].map(|s| s.to_vec()).to_vec();
let m222 = [[5.5, 6.]].map(|s| s.to_vec()).to_vec();
let mul_result3 = multiply_matrix!(&m111, &m222);
assert_eq!(mul_result3, [[5.5, 6.0]]);
rust
use doe::*;
let v1 = vec_element_remove!(vec!["15.", "2.9"], "2.9");
let v2 = vec_element_remove!(vec![15, 2, 3, 2], 2);
let v3 = vec_element_remove!(vec![0.15, 0.2, 0.2], 0.2);
assert_eq!(vec!["15."], v1);
assert_eq!(vec![15, 3, 2], v2);
assert_eq!(vec![0.15, 0.2], v3);
rust
use doe::*;
let v1 = vec_element_position_all!(vec![1, 2, 5, 3, 6, 2, 2], 2);
assert_eq!(v1, vec![1, 5, 6]);
let v2 = vec_element_position_all!(vec!["0".to_string(),"4".to_string(),"7".to_string(),"7".to_string(),], "7".to_string());
assert_eq!(v2, vec![2,3]);
rust
use doe::*;
let v1 = vec_slice!(vec![1.2, 1.5, 9.0], [..2]);
let v2 = vec_slice!(vec![1, 1, 9, 90, 87, 0, 2], [4..6]);
let v3 = vec_slice!(vec![1.2, 1.5, 9.0], [..]);
let v4 = vec_slice!(vec![1.2, 1.5, 9.0], [1..]);
let v5 = vec_slice!(vec!["1", "2", "3", "4", "5"], [2..5]);
let v6 = vec_slice!(vec!["1".to_string(),"2".to_string(),"3".to_string()], [1..]);
assert_eq!(v1, vec![1.2, 1.5]);
assert_eq!(v2, vec![87, 0]);
assert_eq!(v3, vec![1.2, 1.5, 9.0]);
assert_eq!(v4, vec![1.5, 9.0]);
assert_eq!(v5, vec!["3", "4", "5"]);
assert_eq!(v6,vec!["2".to_string(),"3".to_string()]);
rust
use doe::*;
let v1 = vec_element_clone!(vec!["15.", "2.9"], 1);
let v2 = vec_element_clone!(vec![15, 2, 3, 2], 2);
let v3 = vec_element_clone!(vec![0.15, 0.2, 0.2], 0);
let v4 = vec_element_clone!(vec![format!("1"),format!("2"),format!("3"),format!("4"),format!("5")],4);
assert_eq!("2.9", v1);
assert_eq!(3, v2);
assert_eq!(0.15, v3);
assert_eq!(format!("5"), v4);
rust
remove_file_or_folder!("./demo.txt");
remove_file_or_folder!("./target");
rust
use doe::*;
assert_eq!(vec_type!(vec![0.2_f64]), format!("Vec<f64>"));
assert_eq!(vec_type!(vec![0.2_f32]), format!("Vec<f32>"));
assert_eq!(vec_type!(vec![2_i32]), format!("Vec<i32>"));
assert_eq!(vec_type!(vec![2_i128]), format!("Vec<i128>"));
assert_eq!(vec_type!(vec![2_isize]), format!("Vec<isize>"));
assert_eq!(vec_type!(vec![2_usize]), format!("Vec<usize>"));
## convert vec elements type
rust
use doe::*;
let v1: Vec<f64> = vec_element_convert!(vec![1, 2], f64);
let v2: Vec<i32> = vec_element_convert!(vec![1.0, 2.0], i32);
let v3: Vec<i128> = vec_element_convert!(vec![1.0, 2.0], i128);
let v4: Vec<i32> = vec_element_convert!(vec![1_usize, 2_usize], i32);
let v5: Vec<i64> = vec_element_convert!(vec![0.15, 2.0], i64);
assert_eq!(v1, vec![1.0, 2.0]);
assert_eq!(v2, vec![1, 2]);
assert_eq!(v3, vec![1, 2]);
assert_eq!(v4, vec![1, 2]);
assert_eq!(v5, vec![0, 2]);
rust
use doe::*;
let re_max = max!(1, 20);
assert_eq!(re_max,20);
rust
use doe::*;
let re_min = min!(10, 2, 2, 5, 4, 6);
assert_eq!(re_min,2);
rust
use doe::*;
let d1 = binary_to_decimal!("01101",i128);
assert_eq!(d1,13_i128);
let d2 = binary_to_decimal!("00000000000010100110001",i64);
assert_eq!(d2,1329_i64);
let d3 = binary_to_decimal!("000011",i32);
assert_eq!(d3,3_i32);
let d4 = binary_to_decimal!("000101",i16);
assert_eq!(d4,5_i16);
let d5 = binary_to_decimal!("001001",i8);
assert_eq!(d5,9_i8);
rust
use doe::*;
let d1 = binary_to_decimal!("01101",i128);
println!("{:?}",memory_address!(d1));//0x7ffcac734f08
rust
use doe::*;
let v1 = vec_merge!(vec![0, 1, 2], vec![5, 6, 7]);
assert_eq!(vec![0, 1, 2, 5, 6, 7],v1);
let v2 = vec_merge!(vec![0., 1., 2.], vec![5., 6., 7.]);
assert_eq!(vec![0., 1., 2., 5., 6., 7.],v2);
rust
use doe::*;
let v1 = vec_element_take!(vec![0, 1, 2],2);
assert_eq!(vec![0,1],v1);
rust
use doe::*;
let v1 = vec_zip!(vec![0, 1, 2],vec![0, 1, 2]);
assert_eq!(vec![(0,0),(1,1),(2,2)],v1);
rust
use doe::*;
let v1 = vec_enumerate!(vec![12, 11, 20]);
assert_eq!(vec![(0,12),(1,11),(2,20)],v1);
rust
use doe::*;
let v1 = vec_sort!(vec![10, 2, 3]);
assert_eq!(vec![2,3,10],v1);
let v2 = vec_sort!(vec![1.8, 2.5, 0.3]);
assert_eq!(vec![0.3,1.8,2.5],v2);
rust
use doe::*;
let v1 = has_nightly_compiler!();
assert_eq!(v1, true);
rust
use doe::*;
let v1 = has_stable_compiler!();
assert_eq!(v1, false);
rust
use doe::*;
command!("ls -la");
command!("dust");
command!("lsd");
rust
use doe::*;
set_timeout!(||{
println!("ok");
},3000);
rust
use doe::*;
duration_set_timeout!(||{
println!("ok");
},3000,1000); //from now delay 3000ms,each 1000ms run func()
rust
use doe::*;
set_interval!(||{
println!("ok");
},3000);
rust
use doe::*;
let t = utc_timestamp!();
eprintln!("{}", t);
rust
use doe::*;
let t = local_now!();
eprintln!("{}", t);
rust
use doe::*;
let t = utc_now!();
eprintln!("{}", t);
rust
use doe::*;
use std::collections::HashMap;
let mut map:HashMap<i32,i32> = hashmap!();
map.insert(0, 1);
let map1 = hashmap!(2=>5,3=>4);
rust
use doe::*;
use std::collections::BTreeMap;
let mut map:BTreeMap<i32,i32> = btreemap!();
map.insert(0, 1);
let map1 = btreemap!(2=>5,3=>4);