<T> in Rust
发布于 10 天前 作者 yutiansut 53 次浏览 来自 分享

The <T> part, which you’ve seen a few times before, indicates that this is a generic data type. Inside the declaration of our enum, wherever we see a T, we substitute that type for the same type used in the generic. Here’s an example of using Option<T>, with some extra type annotations:

6 回复

! 宏

macro_rules! foo {
	(x => $e:expr) => (println!("mode X: {}", $e));
	(y => $e:expr) => (println!("mode Y: {}", $e));
}
fn main() {
	foo!(y => 3);
}

mode Y: 3

线程

use std::thread;
use std::time::Duration;
fn main() {
thread::spawn(|| {
for i in 1..10 {
println!("hi number {} from the spawned thread!", i);
thread::sleep(Duration::from_millis(1));
}
});
for i in 1..5 {
println!("hi number {} from the main thread!", i);
thread::sleep(Duration::from_millis(1));
}
}

$x:expr 匹配器将会匹配任何 Rust 表达式,把它的语法树绑定到元变量 $x 上

String to f32

let f = “0.11”.parse::<f32>();

macro_rules! array {
    ($([$([$($x:expr),* $(,)*]),+ $(,)*]),+ $(,)*) => {{
        $crate::Array3::from(vec![$([$([$($x,)*],)*],)*])
    }};
    ($([$($x:expr),* $(,)*]),+ $(,)*) => {{
        $crate::Array2::from(vec![$([$($x,)*],)*])
    }};
    ($($x:expr),* $(,)*) => {{
        $crate::Array::from(vec![$($x,)*])
    }};
}
//    let mut args = std::env::args();
//    if args.len() < 2 {
//        eprintln!(
//            "usage: {} [info] [warning] [error]",
//            args.next()
//                .unwrap_or_else(|| "routing_receive_logs_direct".to_string())
//        );
//        std::process::exit(1);
//    }
回到顶部