相关疑难解决方法(0)

我什么时候应该实现std :: convert :: From vs std :: convert :: Into?

我看到它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,但我没有看到它们.

rust

33
推荐指数
1
解决办法
5034
查看次数

无法从另一个使用本地类型参数化的包中从另一个包中实现泛型类型的特征

这个测试代码(围栏):

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

5
推荐指数
1
解决办法
1441
查看次数

标签 统计

rust ×2