|<<>>|66 of 274 Show listMobile Mode

Rust: from zero to pretty-well-versed in 30 minutes

Published by marco on

I found the article A half-hour to learn Rust by Amos to be extremely helpful in learning the syntax and mechanics of Rust.

It starts out with the absolute basics:

let introduces a variable binding […]”

then takes you through

  • Modules
  • Blocks
  • Conditionals
  • Matches
  • Options
  • mutables
  • Copy/clone semantics
  • Traits
  • Generic parameters
  • Constraints
  • Macros
  • Enums
  • Lifetimes and borrowing
  • Generic lifetimes
  • Statics vs. owned vs. referenced
  • Slices and range literals (Index and IndexMut)
  • Results
  • Errors, panic and unwrap, expect() and ?
  • Closures (Fn, FnMut, and FnOnce)
  • move
  • for … in

and ends up with a function builder that tests strings:

fn make_tester<'a>(answer: &'a str) -> impl Fn(&str) -> bool + 'a {
    move |challenge| {
        challenge == answer
    }
}

fn main() {
    let test = make_tester("hunter2");
    println!("{}", test("*******"));
    println!("{}", test("hunter2"));
}

// output:
// false
// true