小编Smo*_*riu的帖子

我应该如何理解&**self in Box

boxed.rs 中的代码

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
impl<T: ?Sized, A: Allocator> const Deref for Box<T, A> {
    type Target = T;

    fn deref(&self) -> &T {
        &**self
    }
}
Run Code Online (Sandbox Code Playgroud)

以我的理解:

  • self-> &Box,
  • *self-> Box
  • **self-> *Box,将调用:*(Box.deref())? 这不会导致递归吗?

我创建一个测试代码:

impl<T> Deref for MyBox<T> {
    type Target = T;

    fn deref(&self) -> &T {
        &**self
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,Rust 返回给我:fatal runtime error: stack overflow

那么&**self …

rust

9
推荐指数
1
解决办法
380
查看次数

标签 统计

rust ×1