Skip to content

Commit 7eb289a

Browse files
Mention struct update syntax
1 parent 4b5728d commit 7eb289a

File tree

1 file changed

+5
-15
lines changed

1 file changed

+5
-15
lines changed

src/language/custom-types/members.md

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -314,23 +314,13 @@ Console.WriteLine(pt.ToString()); // prints: Point { X = 789, Y = 456 }
314314
readonly record struct Point(int X, int Y);
315315
```
316316

317-
There is no `with` in Rust, but to emulate something similar in Rust, it has
318-
to be baked into the type's design:
317+
There equivalence to `with` in Rust is "struct update syntax" or "functional update syntax":
319318

320319
```rust
320+
#[derive(Debug)]
321321
struct Point { x: i32, y: i32 }
322322

323-
impl Point {
324-
pub fn new(x: i32, y: i32) -> Self {
325-
Self { x, y }
326-
}
327-
328-
pub fn x(&self) -> i32 { self.x }
329-
pub fn y(&self) -> i32 { self.y }
330-
331-
// following methods consume self and return a new instance
332-
333-
pub fn set_x(self, val: i32) -> Self { Self::new(val, self.y) }
334-
pub fn set_y(self, val: i32) -> Self { Self::new(self.x, val) }
335-
}
323+
let mut pt = Point { x: 123, y: 456 };
324+
pt = Point { x: 789, ..pt };
325+
println!("{pt:?}");
336326
```

0 commit comments

Comments
 (0)