Rustlings: Live!

Goals:

Share knowledge

Ask Questions

Pair

Grow

Social Rules

https://www.recurse.com/manual

Have Fun!

What is Rust?

What is Rust?

Rust is a statically and strongly typed modern systems programming language focusing on safety, speed, and concurrency. It accomplishes these goals by being memory safe without using garbage collection.

all types are known at compile-time

types are designed to make it harder to write incorrect programs

generating the best possible machine code with full control of memory use

e.g. OS, device drivers

Why Rust vs C/C++?

  • Memory safety w/o garbage collection
  • Clear compiler errors

Why does Rust matter?

traffic mesh

netlify-toml-rs

traffic controller

netlify-headers-rs

...

Top Down Rust

Package Manager npm crates
Version Manager nvm rustup
Registry npmjs.com crates.io
Config package.json cargo.toml
Task Runner npm scripts/gulp make / cargo run 
Linter eslint/tslint clippy
Formatter prettier rustfmt
JavaScript Rust

Installation

$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh

[[ Install Rustup ]]

https://www.rust-lang.org/tools/install

windows

mac

println!("Hello, world!");

├── babys_first_rust/
  ├── main.rs
  └── main

executable

your code

println!("Hello, world!");

fn main() {
    println!("Hello, world!");
}

println!("Hello, world!");

fn main() {
    println!("Hello, world!");
}

4 spaces

end of expression

first code that runs in every executable Rust program

console log macro

println!("Hello {}", SOME_VAR);
println!("Hello {:?}", "world");

Compiling + Executing

rustc main.rs
./main

Cargo

├── babys_first_rust/
  ├── src
    ├── main.rs
  └── cargo.toml

your config

your code

cargo.toml

[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"

[dependencies]

Compiling + Executing

cargo build
./target/debug/hello_cargo

OR

cargo run 

Rust Basics

Variables and Mutability

Variables and Constants

Variables and Shadowing

Variables and Shadowing

Primitive Data Types

Type Example Values
bool true/false
char ❤️, a, 東京
i8, i16, i32, i64, isize -5, 42, 
u8, u16, u32, u64, usize 5, 42
f32, f64 3.14, 4,5
array let array: [i32; 5] = [0, 1, 2, 3, 4];
slice let slice = &array[0..3];
str "Hello World"
tuple let tuple = ("hello", 42, "world", [3,6,9]);

Functions

Functions with Params

Functions with Returns

Conditionals

Conditionals: While

Iterators

Let's Pair!

The rules of raindrops are that if a given number:

  • has 3 as a factor, add 'Pling' to the result.
  • has 5 as a factor, add 'Plang' to the result.
  • has 7 as a factor, add 'Plong' to the result.
  • does not have any of 3, 5, or 7 as a factor, the result should be the digits of the number.
  1. 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
  2. 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang". 

Hints

pub fn raindrops(n: u32) -> String {
  let mut sound = String::new();
  ...
}

Rustlings: Live!

By shortdiv

Rustlings: Live!

  • 577