This is just my attempt at answering (I'm learning too):
Macros are easy to use, allowing beginners to write 'hello world' for example, but hide a bunch of complicated stuff we're unlikely to understand until later. (I vaguely remember from Java that just writing something to the console was a whole bunch statements strung together.)
I found it useful to document what each line was doing in the example, to get my head around the terminology.
std
is a crate, io
is a module in the std
crate, and that module provides methods like stdin()
std
is a crate, cmp
is a module in the std
crate, and that module provides enums like Ordering
rand
is a crate, Rng
is a trait from the rand
crate, and that trait provides methods like thread_rng()
Ordering
is a enum - a type with a list of variants with developer-friendly names. In the same way that a 'DaysOfTheWeek' enum would have 'Monday', 'Tuesday' ..., etc, Ordering
has Less, Greater, or Equal. match
statements work with enums, in a 'for this, do that' kind of way.
Rng
is a trait, which feature a lot in Rust, but is something I've had difficulty getting my head around. Where I'm at so far is they mostly provide convenience. The rand
create provides a number of structures in its rngs
module - ThreadRng
, OsRng
, SmallRng
, and StdRng
, depending on what you want to use to generate randomness. Instead of saying ThreadRng
provides the gen_range()
method, and OsRng
provides the gen_range()
method, etc, Rust just says they implement the Rng
trait. Since Rng
provides the gen_range()
method, it means that everything that implements it will provide it.
thread_rng()
returns a ThreadRng
structure, which provides gen_range()
via the Rng
trait, but we need to bring that trait into scope with the use
keyword in order to be able to call it.
For the default behaviour of passing owned vs. borrowed variables, I guess it's useful to explicitly state "I'm giving this variable to another function, because I don't intend to use it anymore", so that if you inadvertently do, the compiler can catch it and error.