rust

online playground

rust playground

define a vairable

definevar => let|const|static [mut] name [:type] = V;
type => [& [mut] ] | [* [mut]] type
name;

init given size vec with generater function

rust

 let res:Vec<T> = (1..11).map(|_| T::new()).collect(); 

不是很优雅

macro

#[macro_export]
macro_rules! log {
    ($lvl:expr, $($arg:tt)+) => ({
        let meta = format!("{} {} {}",line!(),file!(),module_path!());
        let msg = format_args!($($arg)*);
        println!("{:?} {} {}",$lvl,meta,msg);
    })
}

#[macro_export]
macro_rules! info {
    ($($arg:tt)*) => (log!(crate::util::LogLevel::Info, $($arg)*))
}
  1. macro 会提升到root crate

类型标注

turbofish

let a  = Vec::new::<u32>();

trait

相等性约束(type equality constraints)

//complete the below code
struct A<T>{
    data:Vec<T>
}
impl<T> A<T> {
    fn new<I>(iter:I) ->Self 
    where I:???, 
    {
        let data = iter.collect();
        Self {
            data
        }
    }
}

fn main() {
    let a = A::new(vec![1,2,3].iter());
}
// where I:Iterator<Item=T>, 

rust-play-ground

不同trait 相同函数名

clippy

disable line in some line

#[allow(clippy::wrong_self_convention)] 
fn xxx(){
    
}

dependency

special dependency

[target.'cfg(windows)'.dependencies]
winhttp = "0.4.0"

l-array-range

fn main() {
    let a = vec![1, 2, 3, 4];
    let b = &a[0..1];
    println!("{:?}", a);
    println!("{:?}", b);
}
// [1, 2, 3, 4]
// [1]

类型 引用指针