代码点读机 / Rust / From

From Rust · 常用Trait

安全转换 Trait,表示可以从另一种类型无损失地构造自己;是 Into 的反向。

怎么用

T::from(来源值),用于类型安全转换

小例子

let s = String::from("hello");
println!("{}", s);

运行结果

hello

提醒:From 和 Into 互为镜像,实现了 From 自动获得 Into

官方文档:https://doc.rust-lang.org/std/convert/trait.From.html

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Rust全部词条