Skip to content

Instantly share code, notes, and snippets.

@mrdaemon
Created January 19, 2017 23:11
Show Gist options
  • Save mrdaemon/faffb3c8d6e1e1d634fe15312762d4aa to your computer and use it in GitHub Desktop.
Save mrdaemon/faffb3c8d6e1e1d634fe15312762d4aa to your computer and use it in GitHub Desktop.
// A predictably boring preamble
extern crate rand;
use std::io;
use std::cmp::Ordering;
use rand::Rng;
// Entry point. Returns an empty tuple.
fn main() {
// Get thread rng and poop a number in a range.
// This is the rand crate.
let secret_number = rand::thread_rng().gen_range(1, 101);
// These are macros.
println!("Guess the number, asshole.");
println!("What do you recon?");
//println!("[btw the number is actually {}]", secret_number);
// infinite loop
loop {
// So that's a type instance I guess?
// It's mutable because we need to shit stdin in something, clearly.
let mut guess = String::new();
// .expect() happens because read_line() returns an io::Result instance.
// io::stdin() is essentially a static method that returns a handle to
// the console's stdin. Which has methods, like, unsurprisingly,
// read_line().
//
// Expect on anything that returns a Result is quasi-mandatory.
// The compiler will whine if you don't, which is great.
//
// So far so good.
io::stdin().read_line(&mut guess)
.expect("Wat. Look you need to stdin.");
// To do casts or conversions we can apparently just shadow whatever?
// Also note explicit type annotation
// You can also do matches instead of just a panic expect()
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("That wasn't an integer or anything, but whatever");
continue;
},
};
// Formatting strings works exactly like Python's .format() string method.
// A quick glance tells me there is also a format!() macro.
println!("You guessed: {}", guess);
// This is actually cool shit which I remember from Scala.
// Essentially magic dispatching based on return subtype
// Ordering there is an enum btw.
match guess.cmp(&secret_number) {
Ordering::Less => println!("Like your dick, it's 2 small HAHA"),
Ordering::Greater => println!("2big4me, get more realistic dreams"),
Ordering::Equal => {
println!("YUO ARE A WINRAR!!!1");
println!("--------------------");
println!("Suck my dick, also.");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment