python;gutter:true;
use std::{any::{Any, TypeId}, fmt::Debug};
use downcast_rs::Downcast;</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_n_int(<em>s:Box) -> bool {
// let r = match _s {
// Option::(</em>) =>true,
// _ => false
// };
// r
// }</p>
<p>trait K:Downcast + Debug{
// fn as_any(&self)->T;
}
downcast_rs::impl_downcast!(K);</p>
<p>impl K for i64{
// fn as_any(&self)->T {</p>
<p>// }
}</p>
<p>impl K for f64{
// fn as_any(&self)->Self{
// *self
// }
}</p>
<p>// impl K for String{}</p>
<p>fn tests(c:&mut Box)->bool{
// println!("{:?}", &c);
let b = *c.downcast_mut::().unwrap();
// let cc = b;
println!(">>>>>>>>>>>>>>>>>{:?}", b);</p>
<pre><code>// if is_float(&c){
// println!("it is float");
// }
// if is_string(&c){
// println!("it is strint");
// }
if is_int(&b){
println!("it is int.");
}else{
println!("it is not int");
}
true
</code></pre>
<p>}</p>
<p>trait M:Any+Debug{}</p>
<p>impl M for i64{}</p>
<p>fn testbbb(c:&mut Box)->bool {
let tmp = c.as_any_mut().downcast_mut::();
println!("{:?}", &tmp);</p>
<pre><code>// if is_int(&tmp){
// println!("it is int");
// }else{
// println!("it not int");
// }
true
</code></pre>
<p>}</p>
<p>fn main(){</p>
<pre><code>let mut a:Box = Box::new(5_i64);
let b = tests(&mut a);
let mut c:Box = Box::new(3_i64);
let d = testbbb(&mut c);
// let b:i64 = 5;
// println!("{:?}", a.downcast_ref::());
// let b = match a.downcast_ref::(){
// Some(x) => x,
// _ => None,
// };
// let c = tests(b);
// let mut a = tests(5);
</code></pre>
<p>}
Original: https://www.cnblogs.com/pythonClub/p/16500711.html
Author: CrossPython
Title: rust type
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15796/
转载文章受原作者版权保护。转载请注明原作者出处!