rust type self.

use std::fmt::{Debug, Display};
use std::any::TypeId;
use std::any::Any;
fn is_string<T: ?Sized + Any>(_s: &T) -> bool {
    TypeId::of::<String>() == TypeId::of::<T>()
}
 
fn is_int<T: ?Sized + Any>(_s: &T) -> bool {
    TypeId::of::<i32>() == TypeId::of::<T>()
}
 
fn is_f32<T: ?Sized + Any>(_s: &T) -> bool {
    TypeId::of::<f32>() == TypeId::of::<T>()
}
fn is_f64<T: ?Sized + Any>(_s: &T) -> bool {
    TypeId::of::<f64>() == TypeId::of::<T>()
}
fn is_arr<T: ?Sized + Any>(_s: &T) -> bool {
    TypeId::of::<Vec<i32>>() == TypeId::of::<T>()
}
fn is_str<T: ?Sized + Any>(_s: &T) -> bool {
    TypeId::of::<&str>() == TypeId::of::<T>()
}

trait N:Debug{}

impl N for i32 {}
impl N for f32 {}
impl N for i64 {}
impl N for f64 {}
impl N for String {}
impl N for &str {}
impl N for Vec<i32>{}

fn dosm(c: impl N + 'static){
    // println!("{}", &c);
    if is_f32(&c){
        println!("float32 is");
    }
    if is_f64(&c){
        println!("float64 is");
    }
    if is_int(&c){
        println!("int is");
    }
    if is_string(&c){
        println!("string is");
    }
    if is_arr(&c){
        println!("arr is");
    }
    if is_str(&c){
        println!("str is");
    }
    
}

fn main(){
    let a = vec![1,2,3,4,5,6,7,8,9];
    let b = "A";
    dosm(b);
}

  

posted @ 2022-07-25 22:01  CrossPython  阅读(18)  评论(0编辑  收藏  举报