IMP的原始特征和参考

发布于 2025-02-03 15:02:13 字数 634 浏览 4 评论 0 原文

也许这是一个简单的问题,但是我不明白为什么要对原始的,拥有类型的类型提示特征,我免费获得了参考类型的同一含义...

trait Cool: Sized + std::fmt::Debug {
    fn cool(self) {
        println!("cool -> {:?}", self);
    }
}

impl Cool for i32 {}

// it still works if this line is uncommented...
// impl Cool for &i32 {}

fn main(){
    let val = 123;
    val.cool();
    (&val).cool();
}

playground

Perhaps it is a simple question, but I can't understand why just impl'ing Trait for the primitive, owned type, I got for free the same impl for reference types...

trait Cool: Sized + std::fmt::Debug {
    fn cool(self) {
        println!("cool -> {:?}", self);
    }
}

impl Cool for i32 {}

// it still works if this line is uncommented...
// impl Cool for &i32 {}

fn main(){
    let val = 123;
    val.cool();
    (&val).cool();
}

Playground

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫七 2025-02-10 15:02:13

不仅是因为原始人,它将适用于实现 复制 。否则就不会工作:

trait Cool: Sized + std::fmt::Debug {
    fn cool(self) {
        println!("cool -> {:?}", self);
    }
}

#[derive(Debug)]
struct NonCopy;

impl Cool for i32 {}
impl Cool for NonCopy {}

fn main(){
    let val = 123;
    val.cool();
    (&val).cool();
    
    let nc = NonCopy{};
    nc.cool();
    (&nc).cool();
}

使用明确的错误代码失败:

error[E0507]: cannot move out of a shared reference
  --> src/main.rs:20:5
   |
20 |     (&nc).cool();
   |     ^^^^^^------
   |     |     |
   |     |     value moved due to this method call
   |     move occurs because value has type `NonCopy`, which does not implement the `Copy` trait
   |

Playground

发生的事情是,使用副本类型,Rust在需要时为您透明地创建了副本。

请注意,即使我们评论上一行 // nc.cool(); ,它也会失败,这显然会移动值...

It is not just because of primitives, it will work for all types that implement Copy. Will not work otherwise:

trait Cool: Sized + std::fmt::Debug {
    fn cool(self) {
        println!("cool -> {:?}", self);
    }
}

#[derive(Debug)]
struct NonCopy;

impl Cool for i32 {}
impl Cool for NonCopy {}

fn main(){
    let val = 123;
    val.cool();
    (&val).cool();
    
    let nc = NonCopy{};
    nc.cool();
    (&nc).cool();
}

Fails with a clear error code:

error[E0507]: cannot move out of a shared reference
  --> src/main.rs:20:5
   |
20 |     (&nc).cool();
   |     ^^^^^^------
   |     |     |
   |     |     value moved due to this method call
   |     move occurs because value has type `NonCopy`, which does not implement the `Copy` trait
   |

Playground

What it's happening is that with the Copy types rust creates a copy transparently for you when needed.

Note that it fails even if we comment out the previous line // nc.cool();, which obviously moves the value...

口干舌燥 2025-02-10 15:02:13

这是自动提出的;每当您使用运算符时,它都适用。它的目的是消除 - > 之间的区别,该>在C和相关语言中存在。

它是在

That's auto-dereferencing; it applies whenever you use the . operator. It's meant to erase the distinction between . and -> which exists in C and related languages.

It was introduced in RFC 241.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文