相关疑难解决方法(0)

什么是Rust的确切自动解除引用规则?

我正在学习/试验Rust,在我用这种语言找到的所有优雅中,有一个让我感到困惑并且看起来完全不合适的特点.

在进行方法调用时,Rust会自动取消引用指针.我做了一些测试来确定确切的行为:

struct X { val: i32 }
impl std::ops::Deref for X {
    type Target = i32;
    fn deref(&self) -> &i32 { &self.val }
}

trait M { fn m(self); }
impl M for i32   { fn m(self) { println!("i32::m()");  } }
impl M for X     { fn m(self) { println!("X::m()");    } }
impl M for &X    { fn m(self) { println!("&X::m()");   } }
impl M for &&X   { fn m(self) { println!("&&X::m()");  } }
impl M for &&&X  { …
Run Code Online (Sandbox Code Playgroud)

reference dereference formal-semantics rust

152
推荐指数
2
解决办法
2万
查看次数

Rust中“&*”的作用是什么

从Rust库中读取文档时遇到了这段代码:

for (ent, pos, vel) in (&*entities, &mut pos_storage, &vel_storage).join() {
    println!("Processing entity: {:?}", ent);
    *pos += *vel;
}
Run Code Online (Sandbox Code Playgroud)

原始链接:https : //slide-rs.github.io/specs/08_join.html

&*实体在这里做什么。据我所知,它是在取消引用实体,然后再次引用它?

rust

7
推荐指数
1
解决办法
131
查看次数

标签 统计

rust ×2

dereference ×1

formal-semantics ×1

reference ×1