当前位置: 首页 > news >正文

网站建设工资sem代运营推广公司

网站建设工资,sem代运营推广公司,如何做正版小说网站,现在去云南需要做核酸吗Rust的运行时多态 Rust的静态多态即编译时多态,通过**泛型特征约束(Generic Type Trait Constrait)**来实现; 那么动态多态(运行时多态)呢?答案是特征对象(Trait Object&#xff…

Rust的运行时多态

Rust的静态多态即编译时多态,通过**泛型+特征约束(Generic Type + Trait Constrait)**来实现;

那么动态多态(运行时多态)呢?答案是特征对象(Trait Object)

特征对象不是一个实例,而是一个结构体类型。

语法是dyn TraitName,编译时指示某个对象是实现TraitName的类型,其具体类型未知。

分析

程序运行时,要调用一个特征方法,需要两个要素:

  • 对象实例(对象类型未知,因此其编译期大小未知)
  • 特征的方法表

所以特征对象结构体必须获知以上两个要素。

发生运行时多态时,在编译阶段编译器无法辨别对象实例的类型,因此对象实例要素的大小无法获知,进而特征对象(Trait Object)的大小在编译阶段无法被确定,这决定了Trait Object只能存放在堆上,通过引用或智能指针来访问

指向特征对象的引用或智能指针,包含了两个指针成员,在程序运行时,ptr1在指向对象实例,ptr2指向该对象类型的Trait Method实现。

典型用例


#[derive(Debug)]
struct Journal {author: String,year: u16,from: String,
}
#[derive(Debug)]
struct Conference {author: String,year: u16,country: String,
}trait Summary {fn summary(&self) -> String;
}impl std::fmt::Debug for dyn Summary {fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {write!(f, "{}", self.summary())}
}impl Summary for Journal {fn summary(&self) -> String {format!("{}\t{}\t{}", self.author, self.year, self.from)}
}
impl Summary for Conference {fn summary(&self) -> String {format!("{}\t{}\t{}", self.author, self.year, self.country)}
}// can't defer concrete type in compile stage, trait constrain can't used here
// fn init_default(_type: &str) -> impl Summary {
//     if _type == "journal" {
//         Journal {
//             author: "hjd".to_owned(),
//             year: 2018,
//             from: "Nature".to_owned(),
//         }
//     } else {
//         Conference {
//             author: "hjd".to_owned(),
//             year: 2018,
//             country: "China".to_owned(),
//         }
//     }
// }// 只能使用特征对象进行动态分发,因为返回类型编译期无法推理获知
fn init_default(_type: &str) -> Box<dyn Summary> {if _type == "journal" {Box::new(Journal {author: "hjd".to_owned(),year: 2018,from: "Nature".to_owned(),})} else {Box::new(Conference {author: "hjd".to_owned(),year: 2018,country: "China".to_owned(),})}
}fn main() {let p1 = init_default("journal");let p2 = init_default("conference");let p3 = init_default("journal");let p4 = init_default("conference");let p_list = vec![p1, p2, p3, p4];// dyn Summary是一个特征对象类型,它忘记了自己之前的具体类型,只能调用Summary特征中的方法for p in p_list.iter() {println!("{:?}", p);}
}
http://www.hkea.cn/news/306944/

相关文章:

  • 佛山房地产网站建设seo实战培训王乃用
  • 如何做可以赚钱的网站关键词如何快速排名
  • 深圳品牌做网站公司有哪些百度app推广
  • 重庆建设行业信息网站搜狗登录入口
  • 同仁行业网站建设报价北京做的好的seo公司
  • 陕西自助建站做网站郑州外语网站建站优化
  • 小型企业网站系统cilimao磁力猫最新版地址
  • 铁岭网站建设移动网站广东网站seo
  • 网站模板插件sem和seo
  • 用wordpress制作网站模板沈阳seo
  • 优化一个网站多少钱宜昌网站seo
  • 刚做的网站怎么才能搜索到枸橼酸西地那非片功效效及作用
  • 罗湖区网站公司专业模板建站
  • 哪有备案好的网站国产系统2345
  • 网站开发怎么让别人看到最新营销模式有哪些
  • ssm网站开发源码百度推广多少钱一个月
  • 手游门户网站建设appstore关键词优化
  • 齐河网站开发seo服务内容
  • 北京微信网站建设费用想卖产品怎么推广宣传
  • 网站上线的步骤厦门网站推广公司哪家好
  • 网站做app的软件有哪些百度一下你就知道下载
  • 界面设计的重要性百度seo关键词排名推荐
  • 股票做T网站直播营销
  • 北京手机网站建设公司排名技术优化seo
  • wordpress可爱的主题seo优化教程
  • 自己可以申请网站做外卖吗网站描述和关键词怎么写
  • 公司网站网页设计seo站长工具推广平台
  • 重庆南岸营销型网站建设公司哪家专业真实的网站制作
  • 郑州企业网站建设兼职推广渠道
  • 网站哪些数据优化大师的作用