我看到它std::convert::Into有一个实现任何实现的东西std::convert::From:
impl<T, U> Into<U> for T where U: From<T>
Run Code Online (Sandbox Code Playgroud)
有更具体的实现From,虽然Into目前只有3个特定的实现,这使得它似乎是From默认实现的主流决定.我确信有些时候我只想实现Into而不是From,但我没有看到它们.
这个测试代码(围栏):
use std::fmt::{Display, Formatter, Error};
struct MyLocalType;
type MyResult = Result<MyLocalType, String>;
impl Display for MyResult {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
f.write_str("some test string")
}
}
fn main() {
let r: MyResult = Ok(MyLocalType);
println!("{}" , r);
}
Run Code Online (Sandbox Code Playgroud)
生成此错误消息:
<anon>:7:1: 11:2 error: the impl does not reference any types defined in this crate; only traits defined in the current crate can be implemented for arbitrary types [E0117]
<anon>:7 impl Display for MyResult { …Run Code Online (Sandbox Code Playgroud) rust ×2