Rust 类型、泛型和特征

Rust 创建泛型

// generic_function.rs
fn give_me(value: T)  {
let  _ = value;
}

fn main() {   
 let a = "generics";   
 let b = 1024;   
 give_me(a);  
 give_me(b);
  }

泛型结构体

// generic_struct.rs

struct GenericStruct(T);
struct Container {   
 item: T
}

fn main() {    
// stuff
}

泛型枚举

// generic_enum.rs

enum Transmission {   
 Signal(T),   
  NoSignal
}

fn main() {
    // stuff
 }

Rust 泛型实现

// generic_struct_impl.rs

struct Container {  
  item: T
  }

impl  Container {    
    fn new(item: T) -> Self {    
             Container { item }  
      }
  }

fn main() {
 // stuff
 }

Rust 泛型应用

// using_generic_vec.rs

fn main() {   
 // 提供一种类型    
 let v1: Vec = Vec::new();

你可能感兴趣的:(Rust,rust)