Intro to Rust

Divya

@shortdiv
Software Engineer at Netlify

Goals:

Share knowledge

Ask Questions

Pair

Grow

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?

Top Down Rust

Package Manager npm Go modules crates
Version Manager nvm go get rustup
Registry npmjs.com pkg.go.dev crates.io
Config package.json go.mod cargo.toml
Task Runner npm scripts/gulp make/go run/go build make / cargo run 
Linter eslint/tslint golint clippy
Formatter prettier go fmt rustfmt
JavaScript Go Rust

Installation

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

[[ Install Rustup ]]

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

windows

mac

rustc

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

your code

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 Playground

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");

Rust Basics

Variables and Constants

Variables and Mutability

Variables and Shadowing

Variables and Shadowing

Primitive Data Types

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

Functions

Functions with Params

Functions with Returns

Conditionals

Conditionals: While

Iterators

Let's Work!

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.

Solution

Further Resources

Thank you!

Rustlings: Live!

By shortdiv

Rustlings: Live!

  • 611