mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-23 23:50:25 +08:00
Clarify auto ref/deref
This commit is contained in:
parent
6947ef48cc
commit
38650800f8
@ -73,16 +73,19 @@ of our code search for capabilities of `Rectangle` all over the place.
|
|||||||
|
|
||||||
#### Where's the `->` operator?
|
#### Where's the `->` operator?
|
||||||
|
|
||||||
In C++, there are two different operators for calling methods: `.` if you're
|
In languages like C++, there are two different operators for calling methods:
|
||||||
calling a method on the object directly, and `->` if you're calling the method
|
`.` if you're calling a method on the object directly, and `->` if you're
|
||||||
on a pointer to the object and thus need to dereference the pointer first. Rust
|
calling the method on a pointer to the object and thus need to dereference the
|
||||||
doesn't have an equivalent to the `->` operator; instead, Rust has a feature
|
pointer first. In other words, if `object` is a pointer, `object->something()`
|
||||||
called *automatic referencing*. Calling methods is one of the few places in
|
is like `(*object).something()`.
|
||||||
Rust that has behavior like this.
|
|
||||||
|
|
||||||
Here’s how it works: when you call a method with `object.something(`, Rust will
|
Rust doesn't have an equivalent to the `->` operator; instead, Rust has a
|
||||||
automatically add in `&`s or `&mut`s so that `object` matches the signature of
|
feature called *automatic referencing and dereferencing*. Calling methods is
|
||||||
the method. In other words, these are the same:
|
one of the few places in Rust that has behavior like this.
|
||||||
|
|
||||||
|
Here’s how it works: when you call a method with `object.something()`, Rust
|
||||||
|
will automatically add in `&`, `&mut`, or `*` so that `object` matches the
|
||||||
|
signature of the method. In other words, these are the same:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# #[derive(Debug,Copy,Clone)]
|
# #[derive(Debug,Copy,Clone)]
|
||||||
|
Loading…
Reference in New Issue
Block a user