Update Examples
parent
4789024f4c
commit
0914ebd8d3
127
Examples.md
127
Examples.md
@ -193,3 +193,130 @@ Not really anything new in this subsection; we don't currently expect that Husht
|
||||
### Custom Types
|
||||
|
||||
#### Structures
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Rust</th>
|
||||
<th>Husht</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
```
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Person {
|
||||
name: String,
|
||||
age: u8,
|
||||
}
|
||||
|
||||
struct Unit;
|
||||
|
||||
struct Pair(i32, f32);
|
||||
|
||||
struct Point {
|
||||
x: f32,
|
||||
y: f32,
|
||||
}
|
||||
|
||||
struct Rectangle {
|
||||
top_left: Point,
|
||||
bottom_right: Point,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Create struct with field init shorthand
|
||||
let name = String::from("Peter");
|
||||
let age = 27;
|
||||
let peter = Person { name, age };
|
||||
|
||||
println!("{:?}", peter);
|
||||
|
||||
let point: Point = Point { x: 10.3, y: 0.4 };
|
||||
let another_point: Point = Point { x: 5.2, y: 0.2 };
|
||||
|
||||
println!("point coordinates: ({}, {})", point.x, point.y);
|
||||
|
||||
let bottom_right = Point { x: 5.2, ..another_point };
|
||||
|
||||
println!("second point: ({}, {})", bottom_right.x, bottom_right.y);
|
||||
|
||||
// Destructure the point using a `let` binding
|
||||
let Point { x: left_edge, y: top_edge } = point;
|
||||
|
||||
let _rectangle = Rectangle {
|
||||
top_left: Point { x: left_edge, y: top_edge },
|
||||
bottom_right: bottom_right,
|
||||
};
|
||||
|
||||
let _unit = Unit;
|
||||
|
||||
let pair = Pair(1, 0.1);
|
||||
|
||||
println!("pair contains {:?} and {:?}", pair.0, pair.1);
|
||||
|
||||
// Destructure a tuple struct
|
||||
let Pair(integer, decimal) = pair;
|
||||
|
||||
println!("pair contains {:?} and {:?}", integer, decimal);
|
||||
}
|
||||
```
|
||||
</td><td>
|
||||
|
||||
```
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Person
|
||||
name: String
|
||||
age: u8
|
||||
|
||||
struct Unit
|
||||
|
||||
struct Pair(i32, f32)
|
||||
|
||||
struct Point x: f32, y: f32
|
||||
|
||||
struct Rectangle
|
||||
top_left: Point
|
||||
bottom_right: Point
|
||||
|
||||
fn main()
|
||||
// Create struct with field init shorthand
|
||||
let name = s"Peter" // note s-string
|
||||
let age = 27
|
||||
let peter = Person name, age
|
||||
|
||||
println! "{:?}", peter
|
||||
|
||||
let point: Point = Point x: 10.3, y: 0.4
|
||||
let another_point: Point = Point x: 5.2, y: 0.2
|
||||
|
||||
println! "point coordinates: ({}, {})", point.x, point.y
|
||||
|
||||
let bottom_right = Point x: 5.2, ..another_point
|
||||
|
||||
println! "second point: ({}, {})", bottom_right.x, bottom_right.y
|
||||
|
||||
// Destructure the point using a `let` binding
|
||||
let Point x: left_edge, y: top_edge = point;
|
||||
|
||||
let _rectangle = Rectangle
|
||||
top_left: Point
|
||||
x: left_edge, y: top_edge
|
||||
bottom_right: bottom_right
|
||||
|
||||
let _unit = Unit
|
||||
|
||||
let pair = Pair 1, 0.1
|
||||
|
||||
println! "pair contains {:?} and {:?}", pair.0, pair.1
|
||||
|
||||
// Destructure a tuple struct
|
||||
let Pair integer, decimal = pair;
|
||||
|
||||
println! "pair contains {:?} and {:?}", integer, decimal
|
||||
|
||||
```
|
||||
</td></tr></table>
|
Loading…
Reference in New Issue
Block a user