So Jersh is
writing a new forum system with blackjack and hookers in Rust. I don't really know much about Rust so let's have a look, shall we?
From this docs page:
Code:
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1..101);
println!("The secret number is: {}", secret_number);
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => println!("You win!"),
}
}
The
io::stdin().read_line()
bit is interesting. It's returning an enum with either "Ok" or "Err" "variants" (as Rust calls them), and in the case of the latter, calling the
expect()
method causes the program to crash after outputting the passed string as an error message. That's about the only idea here that I like.
What sucks?
println!()
is an obvious one. Apparently methods with exclamation marks in Rust are actually macros; the code around its parameters gets expanded to several other lines of code when compiled.
The function to print a line to the terminal is not a built-in or stdlib function. Apparently this is necessary because as you see
println!()
does both line-printing and string formatting, and Rust doesn't natively support variadic functions (functions with an arbitrary/undefined number of parameters), but they can be faked with macros. And rather than just having it be a function which takes a string and an array of strings to do the placeholder replacement with or some other sane alternative, they decided they'd implement it with macros and you can just litter your code with exclamation points and fuck off.
Next, the intermingling of OOP-ish concepts with snake_case. We Ruby now bois. Speaking of which, I'm confused what significance the capitalization has in the namespacing. I suppose I can look it up like I did with the exclamation points but I'm getting annoyed already. And why do we
use rand::Rng;
but then never actually use Rng anywhere?
let mut guess
. The "mut" keyword makes the variable mutable. I don't like the idea of immutable variables because we already have a word for those and it is "constant." JavaScript actually does something right by having separate
let
and
const
keywords for variables and constants rather than keyword-stacking.
&
. We're explicitly passing by reference twice in this bitty code sample. I much prefer languages that just pass everything by reference, but whatever; why in the first instance do we have to do
&mut guess
instead of just
&guess
?
For now, all you need to know is that like variables, references are immutable by default. Hence, you need to write &mut guess
rather than &guess
to make it mutable.
…Fuck me with a shovel. If you pass a variable by reference,
it becomes a constant unless you add a keyword to make it otherwise. The fuck?
Well, whatever. I guess we'll finish up by doing enum bullshit to compare the guessed number with the random number because
>
and
<
and
==
are for plebs, fuck you.
Rust at first glance looks noisy and over-complicated - much like the troons that use it, I guess.