Skip to content

Commit a0377dc

Browse files
authored
Merge pull request #837 from hngnaig/master
Add example struct field init shorthand
2 parents 576efaf + 87de92e commit a0377dc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

examples/custom_types/structs/structs.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#[derive(Debug)]
2+
struct Person<'a> {
3+
name: &'a str,
4+
age: u8
5+
}
6+
17
// A unit struct
28
struct Nil;
39

@@ -18,6 +24,15 @@ struct Rectangle {
1824
}
1925

2026
fn main() {
27+
// Create struct with field init shorthand
28+
let name = "Peter";
29+
let age = 27;
30+
let peter = Person { name, age };
31+
32+
// Print debug struct
33+
println!("{:?}", peter);
34+
35+
2136
// Instantiate a `Point`
2237
let point: Point = Point { x: 0.3, y: 0.4 };
2338

0 commit comments

Comments
 (0)