当前位置:嗨网首页>书籍在线阅读

33-From和Into

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

7.6.4 From和Into

实现将一种类型转换为另一种类型,我们可用From和Into特征。关于这两个特征有趣的部分是,我们只需实现From特征就能自动获得Into特征的实现,例如以下impl代码块:

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, U> Into<U> for T where U: From<T> {
    fn into(self) -> U {
        U::from(self)
    }
}