my Rust sandbox and Rust Exercism exercises

freeCodeCamp youTube tutorial

the book

rust by practice

rust-lang online sandbox

install

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

init

cargo new hello-rust

CLI bundling

cargo install cargo-edit
cargo add unicode-segmentation

backtracing

RUST_BACKTRACE=1 cargo run
RUST_BACKTRACE=full cargo run

Assetion

assert!(0.1 as f32 + 0.2 as f32 == 0.3 as f32);
assert!(0.1_f32 + 0.2_f32 == 0.3_f32);

types

Integers and Floats

image

defaults

int: i32 float: f64

limits

image

Range

    for c in 'a'..='z' { // `..='z'` means included, like `...'z'` in Ruby
        println!("{}",c as u8); // c as u8 converrts to ascii
    }

assert_eq!((1..5), Range{ start: 1, end: 5 });
assert_eq!((1..=5), RangeInclusive::new(1, 5));

Bitwise Operations

println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
println!("1 << 5 is {}", 1u32 << 5);
println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);

0011 AND 0101 is 0001
0011 OR 0101 is 0111
0011 XOR 0101 is 0110
1 << 5 is 32
0x80 >> 2 is 0x20

image

Char

let c1: char = '中';
let c1: &str = "中"; // double quotes

Leave a comment