Install Rust
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | shUpdate Rust
rustup updateCreate new project
cargo new myappcd myappcargo run
Compile and run
cargo run (will compile and run main.rs)
build release bin
cargo build --releaseremove libscargo cleanshow all installed libscargo --listConditionals
if / else
fn main() { let n = 5;AND, OR
if n < 10 && n > 10IF NOT
if ! name = "Frank"
if name != "Frank"
User Input
pass value of user input to &mut guess = Reference object
use std::io;fn main {install cargo pkgs
vi Cargo.toml
Update dependencies
cargo update
convert string literal to String object
remote whitespace
let mut name = String::from(" hello");print variables
println!("pattern: {:?}, path: {:?}", args.pattern, args.path);let t = true;
let f: bool = false;
func with return type
fn calculate_length(s: &String) -> i32 {declare new struct
struct User { active: bool, username: String, email: String, sign_in_count: u64,}init the struct
struct builder function
fn build_user(email: String, username: String) -> User { User { active: true, username, email, sign_in_count: 1, }}Tuple Structs (no field names)
struct Color(i32, i32, i32);struct Point(i32, i32, i32);add debug to Struct
#[derive(Debug)]struct Rectangle { width: u32, height: u32,}Variables
new mutable string
let mut name = String::new();bool
char
f32,f64 // 32bit, 64bit float
i8,i16,i32,i64,i128 // signed int
i8 = -127 to 127
i16 =
u8,u16,u32,u64,u128 //unsigned 0 to 255, no negative
Object reference
let s1 = String::from("hello");let len = calculate_length(&s1);len is reference to value of s1, no ownership
Integers
signed/unsigned integers (signed can be negative)
8 bit = i8 (-127 to 127), u8 (0 to 255)
16 bit = i16 (, u16 (-
32 bit = i32, u32
64 bit = i64, u64
128 bit = i128, u128
arch = isize, usize
floating point
f32, f64 (f64 is default)
let number = 2.0;
faster than Vector, sits on stack not heap
declare type + size of arraylet a: [i32; 5] = [1, 2, 3, 4, 5];access element of array
let a = [1,2,3,4,5];let idx = 3;println!("{}", a[idx]);
convert String to Int
Conditionals / For Loops
infinite loop
loop {
println!("stuff");
}