mopa = 0.2
python;gutter:true;
use mopa;
use std::fmt::Debug;
use std::fmt::Display;
use std::any::TypeId;</p>
<p>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_float(_s: &T) -> bool {
TypeId::of::() == TypeId::of::()
}</p>
<p>fn is_str(_s: &T) -> bool {
TypeId::of::() == TypeId::of::()
}</p>
<p>trait N: mopa::Any+Debug+Display{}</p>
<p>impl N for i64 {}
impl N for f64 {}
impl N for String {}
impl N for &'static str {}</p>
<p>fn dosm(c: impl N){
println!("{}", c);
if is_int(&c){
println!("yes, int");
}
if is_float(&c){
println!("float, it is,");
}
if is_string(&c){
println!("it is string");
}
if is_str(&c){
println!("it is str");
}
}</p>
<p>fn main() {
let a = String::from("hello");
let a1 = "A";
dosm(a1);
}
Original: https://www.cnblogs.com/pythonClub/p/16518403.html
Author: CrossPython
Title: rust mopa2
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15788/
转载文章受原作者版权保护。转载请注明原作者出处!