Skip to content

Instantly share code, notes, and snippets.

@dumindu
Last active January 8, 2017 10:46
Show Gist options
  • Save dumindu/9f9bec072612c227f9fa27f8755f212b to your computer and use it in GitHub Desktop.
Save dumindu/9f9bec072612c227f9fa27f8755f212b to your computer and use it in GitHub Desktop.
struct Color (u8, u8, u8);
struct Kilometers(i32);
fn main() {
// creating an instance
let black = Color (0, 0, 0);
// destructure the instance using a `let` binding, this will not destruct black instance
let Color (r, g, b) = black;
println!("Black = rgb({}, {}, {})", r, g, b); //black = rgb(0, 0, 0);
//newtype pattern
let distance = Kilometers(20);
// destructure the instance using a `let` binding
let Kilometers(distance_in_km) = distance;
println!("The distance: {} km", distance_in_km); //The distance: 20 km
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment