@shortdiv
Software Engineer at Netlify
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
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 |
---|
$ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
[[ Install Rustup ]]
https://www.rust-lang.org/tools/install
windows
mac
├── babys_first_rust/
└── src
└── main.rs
your code
rustc main.rs
./main
├── babys_first_rust/
├── src
├── main.rs └── cargo.toml
your config
your code
[package] name = "hello_cargo" version = "0.1.0" authors = ["Your Name <you@example.com>"] edition = "2018" [dependencies]
cargo build
./target/debug/hello_cargo
OR
cargo run
fn main() { 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");
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]); |
The rules of raindrops are that if a given number: