File tree Expand file tree Collapse file tree 1 file changed +5
-15
lines changed
src/language/custom-types Expand file tree Collapse file tree 1 file changed +5
-15
lines changed Original file line number Diff line number Diff line change @@ -314,23 +314,13 @@ Console.WriteLine(pt.ToString()); // prints: Point { X = 789, Y = 456 }
314
314
readonly record struct Point (int X , int Y );
315
315
```
316
316
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":
319
318
320
319
``` rust
320
+ #[derive(Debug )]
321
321
struct Point { x : i32 , y : i32 }
322
322
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:?}" );
336
326
```
You can’t perform that action at this time.
0 commit comments