mirror of
https://github.com/rust-lang/rust
synced 2024-11-02 10:21:05 +00:00
parent
3860240b0e
commit
f78ee1aff1
1 changed files with 30 additions and 0 deletions
|
@ -87,3 +87,33 @@ fn main() {
|
|||
point.y = 6; // this causes an error
|
||||
}
|
||||
```
|
||||
|
||||
# Update syntax
|
||||
|
||||
A `struct` can include `..` to indicate that you want to use a copy of some
|
||||
other struct for some of the values. For example:
|
||||
|
||||
```rust
|
||||
struct Point3d {
|
||||
x: i32,
|
||||
y: i32,
|
||||
z: i32,
|
||||
}
|
||||
|
||||
let mut point = Point3d { x: 0, y: 0, z: 0 };
|
||||
point = Point3d { y: 1, .. point };
|
||||
```
|
||||
|
||||
This gives `point` a new `y`, but keeps the old `x` and `z` values. It doesn’t
|
||||
have to be the same `struct` either, you can use this syntax when making new
|
||||
ones, and it will copy the values you don’t specify:
|
||||
|
||||
```rust
|
||||
# struct Point3d {
|
||||
# x: i32,
|
||||
# y: i32,
|
||||
# z: i32,
|
||||
# }
|
||||
let origin = Point3d { x: 0, y: 0, z: 0 };
|
||||
let point = Point3d { z: 1, x: 2, .. origin };
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue