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)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总