kota's memex

let and mut

let mut guess = String::new();

This creates a mutable string named "guess".

let apples = 5;

This creates an immutable variable called "apples" and binds it to the value 5.

constants

Like immutable variables, constants are values bound to a name which are not allowed to change. Constants can be declared in any scope, including the global scope. They must be set to a constant expression which can be evaluated at compile time. A constant is valid for the entire time the program runs, within the scope it was declared.

const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 ^ 3;

type annotations

Normally, rust can infer the type of a variable based on its usage, but in certain cases, such as when converting a string to a numeric type with .parse() we must annotate the desired type.

let guess: u32 = "42".parse().expect("Not a number!");

shadowing

Rust supports variable shadowing. So you can redefine a variable in the same block with a different type, shadowing the original.

comparable

If a type is comparable, you can use the .cmp method on it:

assert_eq!(5.cmp(&10), Ordering::Less);
assert_eq!(10.cmp(&5), Ordering::Greater);
assert_eq!(5.cmp(&5), Ordering::Equal);

It's common to combine that with the match statement.

naming

By convention functions and variables use snake_case in rust.