RUST Learning Note
RUST learning note
1. Installation
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Set default channel, ex. “stable”: rustup default stable
Update: rustup update
Uninstall Rust: rustup self uninstall
2. Data type
Data type should be defined when it’s build, unlike script languages.
- Integer: u8 -> i128
- Float: f32, f64
- Bool: true, false. 0 cannot be taken as false.
- Char: ‘a’
- Tuple: (int, bool, char, float)
- Array: (int, int, …), (float, float, …)
- Vector: Similar to Array, but length is changable
- HashMap(key, val): for storage
- BTreeMap(key, val): can compare elements, less efficient when insert item
- HashSet(val)
- BTreeSet(val)
- Enum: (val(Type)), can assign defferent types for values
3. Logic control
- if, else if
- match
match a { 0 => println!("zero") 1..=i32::MAX => println!("positive") _ => println!("negative") } - loop: need “break;” to get out
- while
- for: ex.
for num in 1..5, 5 is not iterated4. Exception
- Option: check null pointer
- Result: check return value of process
- panic
use std::panic panic!("Un-solvable error")5. Cargo
Cargo is a package management tool. Useful commands:
Cargo new $PROJECT_NAME <--lib> Cargo build <--release> Cargo run Cargo check # check dependencies Cargo test # unit test - Cargo.toml Toml file defines build content, like a cmake file ```toml [package] name = “example” version = “0.1.0” authors = [“someone someone@someorg.com”] edition = “2022”
[[bin]] name = “example_bin” path = “src/main.rs”
[dependencies] … ```