rust type self.

python;gutter:true; use std::fmt::{Debug, Display}; use std::any::TypeId; use std::any::Any; fn is_string(_s: &T) -> bool { TypeId::of::() == TypeId::of::() }</p> <p>fn is_int(_s: &T) -> bool { TypeId::of::() == TypeId::of::() }</p> <p>fn is_f32(_s: &T) -> bool { TypeId::of::() == TypeId::of::() } fn is_f64(_s: &T) -> bool { TypeId::of::() == TypeId::of::() } fn is_arr(_s: &T) -> bool { TypeId::of::>() == TypeId::of::() } fn is_str(_s: &T) -> bool { TypeId::of::() == TypeId::of::() }</p> <p>trait N:Debug{}</p> <p>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{}</p> <p>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"); }</p> <p>}</p> <p>fn main(){ let a = vec![1,2,3,4,5,6,7,8,9]; let b = "A"; dosm(b); }

Original: https://www.cnblogs.com/pythonClub/p/16519031.html
Author: CrossPython
Title: rust type self.

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15786/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

发表回复

登录后才能评论
免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部