Skip to content

Instantly share code, notes, and snippets.

@majudhu
Last active May 14, 2021 14:51
Show Gist options
  • Save majudhu/bb85a3c9a991ebfe7d5675fd6cffdce6 to your computer and use it in GitHub Desktop.
Save majudhu/bb85a3c9a991ebfe7d5675fd6cffdce6 to your computer and use it in GitHub Desktop.
Rust Snippets
#[derive(Debug)]
enum Fruits {
Apple,
Banana,
Orange,
Gauva,
}
#[derive(Debug)]
enum Vegetables {
Carrot,
Cabbage,
Potato,
Tomato,
}
#[derive(Debug)]
enum Junk {
Pizza,
Burger,
Submarine,
}
#[derive(Debug)]
enum Food {
Fruit(Fruits),
Vegetable(Vegetables),
Junk(Junk),
}
#[derive(Debug)]
struct Basket {
item1: Food,
item2: Food,
item3: Food,
item4: Food,
}
fn main() {
let snack1 = Food::Fruit(Fruits::Apple);
let snack2 = Food::Vegetable(Vegetables::Cabbage);
println!("{:?}, {:?}", snack1, snack2);
let gift = Basket {
item1: snack1,
item2: snack2,
item3: Food::Junk(Junk::Pizza),
item4: Food::Fruit(Fruits::Gauva),
};
println!("{:?}", gift);
let mut list: Vec<Food> = vec![
gift.item1,
gift.item3,
Food::Vegetable(Vegetables::Carrot),
Food::Fruit(Fruits::Orange),
];
println!("{:?}", &list);
for item in &list {
match item {
Food::Fruit(fruit) => {
println!("Fruit is good");
match fruit {
Fruits::Apple => println!("Apple, tasty"),
Fruits::Banana => println!("nananananan"),
Fruits::Gauva => println!("gogogogog"),
Fruits::Orange => println!("sour"),
}
}
Food::Vegetable(vegetable) => {
println!("{:?} is a vegetable", vegetable);
}
Food::Junk(junk) => {
println!("ew junk");
if let Junk::Pizza = junk {
println!("can i haz pizza")
}
}
}
}
list.push(Food::Fruit(Fruits::Banana));
let just_fruits = list
.into_iter()
.filter(|item| {
if let Food::Fruit(_) = item {
true
} else {
false
}
})
.map(|item| match item {
Food::Fruit(fruit) => fruit,
_ => panic!(),
})
.collect::<Vec<Fruits>>();
println!("{:?}", just_fruits);
let just_junk = [
Food::Junk(Junk::Burger),
Food::Vegetable(Vegetables::Tomato),
Food::Vegetable(Vegetables::Potato),
Food::Junk(Junk::Submarine),
]
.iter()
.filter_map(|item| match item {
Food::Junk(junk) => Some(junk),
_ => None,
})
.collect::<Vec<&Junk>>();
println!("{:?}", just_junk);
}
trait Dog {
fn bark(&self);
}
trait Duck {
fn quack(&self);
}
trait Bird {
fn chirp(&self);
}
trait Cat {
fn meow(&self);
}
trait Car {
fn horn(&self);
}
struct Monster {}
impl Dog for Monster {
fn bark(&self) {
println!("Woof woof");
}
}
impl Duck for Monster {
fn quack(&self) {
println!("Quack quack");
}
}
impl Bird for Monster {
fn chirp(&self) {
println!("Chirp chirp");
}
}
impl Cat for Monster {
fn meow(&self) {
println!("Meow meow");
}
}
impl Car for Monster {
fn horn(&self) {
println!("Peep peep");
}
}
fn growl_monste_<T: Dog + Duck + Bird + Cat + Car>(monster: &T) {}
fn growl_monster(monster: &(impl Dog + Duck + Bird + Cat + Car)) {
monster.bark();
monster.quack();
monster.chirp();
monster.meow();
monster.horn();
}
fn main() {
let monster = Monster {};
growl_monster(&monster);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment