# What is Rust? - A modern low level language almost as fast as C/C++ - Compiler try make as safe code as possible at compile time with stricter coding rules. ---- # At compile time Rust has: - Built in code analyze: - Avoid leaks with help of borrow checker - Avoid race conditions (variable can only be borrowed as mut once only.) - Avoid buffer over/under flows in std libbraries and arrays. - unsafe must be *opted in* and can be used when using external C libraries. --- # Non mutable as default ```rust fn main() { let foo = 1; println!("foo is: {}", foo); foo = 2; // Compile fails cause we aren't allowed to change it println!("foo is: {}", foo); } // try again... fn main() { let mut foo = 1; println!("foo is: {}", foo); foo = 2; println!("foo is: {}", foo); } ``` --- # Rust checks life time of the data at compile time ```rust fn add_foo(foo: usize) { foo += 1; println!("foo is in add_foo: {}") // foo is "pop'ed" from stack } fn main() { let foo = 1; println!("foo is before call: {}", foo); add_foo(mut foo); // function takes ownerchip of foo println!("foo is: {}", foo); // <= Compile fails } ``` --- # Built in borrow checker Make sure data is valid. A function can borrow an variable and change it but only one mutation is allowed. ```rust fn add_foo(foo: &mut usize) { *foo += 1; } fn main() { let mut foo = 1; println!("foo is before call: {}", foo); add_foo(&mut foo); // here we borrow the data // and now it still exists on stack since function did only borrow it println!("foo is: {}", foo); } ``` # Cargo the modern make Fetches the packages used in the project gitlab/github etc... or local path's. - cargo build/run - Cargo.toml the Makefile ## Some cargo plugins: - *cargo fmt* should be run in vim/emacs to avoid endless discussions about code formatting. - *cargo clippy* gives plenty of tips how you may make code better - *cargo deb* (creates debian packages) - Cargo fmt - Standardnized code formatting to avoid endless discussions when code review.
{"metaMigratedAt":"2023-06-15T20:57:07.186Z","metaMigratedFrom":"Content","title":"What is Rust?","breaks":true,"contributors":"[{\"id\":\"e8c19b6e-b4ec-45d1-8256-e55f18239934\",\"add\":2148,\"del\":0}]"}
    122 views