Skip to content

Instantly share code, notes, and snippets.

@kenjiSpecial
Created July 1, 2020 13:35
Show Gist options
  • Save kenjiSpecial/69ed3a0802f59938ef8e3a2c2e1617e3 to your computer and use it in GitHub Desktop.
Save kenjiSpecial/69ed3a0802f59938ef8e3a2c2e1617e3 to your computer and use it in GitHub Desktop.
/*--------------------------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
*-------------------------------------------------------------------------------------------------------------*/
struct StA {
a: i64,
b: i64,
}
enum WebEvent {
// An `enum` may either be `unit-like`,
PageLoad,
PageUnload,
// like tuple structs,
KeyPress(char),
Paste(String),
// or c-like structures.
Click { x: i64, y: i64 },
Touch { x: i64, y: i64, z: i64 },
T(StA),
}
// A function which takes a `WebEvent` enum as an argument and
// returns nothing.
fn inspect(event: WebEvent) {
match event {
WebEvent::PageLoad => println!("page loaded"),
WebEvent::PageUnload => println!("page unloaded"),
// Destructure `c` from inside the `enum`.
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);
}
WebEvent::Touch { x, y, z } => {
println!("clicked at x={}, y={}, z={}", x, y, z);
}
WebEvent::T(st) => {
println!("StA, a={}, b={}", st.a, st.b);
}
}
}
fn main() {
let pressed = WebEvent::KeyPress('x');
// `to_owned()` creates an owned `String` from a string slice.
let pasted = WebEvent::Paste("my text".to_owned());
let click = WebEvent::Click { x: 20, y: 80 };
let touch = WebEvent::Touch {
x: 20,
y: 80,
z: 100,
};
let load = WebEvent::PageLoad;
let unload = WebEvent::PageUnload;
let st = WebEvent::T(StA { a: 10, b: 10 });
inspect(pressed);
inspect(pasted);
inspect(click);
inspect(load);
inspect(unload);
inspect(touch);
inspect(st);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment