Update Examples
parent
a0684c6628
commit
b319018f6d
94
Examples.md
94
Examples.md
@ -324,3 +324,97 @@ fn main()
|
||||
|
||||
```
|
||||
</td></tr></table>
|
||||
|
||||
#### Enums
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Rust</th>
|
||||
<th>Husht</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
```
|
||||
enum WebEvent {
|
||||
// An `enum` variant may either be `unit-like`,
|
||||
PageLoad,
|
||||
PageUnload,
|
||||
// like tuple structs,
|
||||
KeyPress(char),
|
||||
Paste(String),
|
||||
// or like structures.
|
||||
Click { x: i64, y: i64 },
|
||||
}
|
||||
|
||||
fn inspect(event: WebEvent) {
|
||||
match event {
|
||||
WebEvent::PageLoad => println!("page loaded"),
|
||||
WebEvent::PageUnload => println!("page unloaded"),
|
||||
// Destructure `c` from inside the `enum` variant.
|
||||
WebEvent::KeyPress(c) => println!("pressed '{}'.", c),
|
||||
WebEvent::Paste(s) => println!("pasted \"{}\".", s),
|
||||
// Destructure `Click` into `x` and `y`.
|
||||
WebEvent::Click { x, y } => {
|
||||
println!("clicked at x={}, y={}.", x, y);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let pressed = WebEvent::KeyPress('x');
|
||||
let pasted = WebEvent::Paste("my text".to_owned());
|
||||
let click = WebEvent::Click { x: 20, y: 80 };
|
||||
let load = WebEvent::PageLoad;
|
||||
let unload = WebEvent::PageUnload;
|
||||
|
||||
inspect(pressed);
|
||||
inspect(pasted);
|
||||
inspect(click);
|
||||
inspect(load);
|
||||
inspect(unload);
|
||||
}
|
||||
```
|
||||
</td><td>
|
||||
|
||||
```
|
||||
enum WebEvent
|
||||
// An `enum` variant may either be `unit-like`,
|
||||
PageLoad,
|
||||
PageUnload,
|
||||
// like tuple structs,
|
||||
KeyPress(char),
|
||||
Paste(String),
|
||||
// or like structures.
|
||||
Click
|
||||
x: i64, y: i64
|
||||
|
||||
fn inspect(event: WebEvent)
|
||||
match event // Note variant auto-import
|
||||
PageLoad => println! "page loaded"
|
||||
PageUnload => println! "page unloaded"
|
||||
// Destructure `c` from inside the `enum` variant.
|
||||
KeyPress c => println! "pressed '{}'.", c
|
||||
Paste s => println! "pasted \"{}\".", s
|
||||
// Destructure `Click` into `x` and `y`.
|
||||
Click x, y =>
|
||||
println! "clicked at x={}, y={}.", x, y
|
||||
|
||||
|
||||
|
||||
|
||||
fn main()
|
||||
let pressed = WebEvent::KeyPress 'x'
|
||||
pasted = WebEvent::Paste s"my text"
|
||||
click = WebEvent::Click x: 20, y: 80
|
||||
load = WebEvent::PageLoad
|
||||
unload = WebEvent::PageUnload
|
||||
|
||||
inspect pressed
|
||||
inspect pasted
|
||||
inspect click
|
||||
inspect load
|
||||
inspect unload
|
||||
|
||||
```
|
||||
</td></tr></table>
|
Loading…
Reference in New Issue
Block a user