Newer
Older
During all the sessions, I talked about quality in terms of code validation: you write tests to check your program; you build Docker images to provide everybody with a packaged environment.
But, quality also comes with the programming language itself.
During the last years, Rust emerged as the programming language to build (more) robust, safe software systems (compared to C++ and C).
Mozilla created Rust to replace C++ in the development of Firefox. Their goal is to have a language more focused on memory access and errors.
Rust has no garbage collector at run time, compiles to native code, and you do not have to delete pointers:
the memory model of rust is defined such a way the compiler can infer where pointers muste be freed. Mozilla created Rust to limit memory issues and attacks on Firefox.
See: https://9to5google.com/2022/12/01/android-memory-safety-rust/
Rust may make C++ and C deprecated during the coming decade(s).
## Installation
Rust installer: https://rustup.rs
Then, `rustup default stable`
Then, install the tutorials:
`curl -L https://raw.githubusercontent.com/rust-lang/rustlings/main/install.sh | bash`
You can also run Rust code directly on:
https://play.rust-lang.org/
## A first example
Copy the following code in https://play.rust-lang.org/ and run
```rs
fn main() {
let mut vec0: Vec<i32> = Vec::new();
vec0.push(1);
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
vec0.push(88);
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
}
fn foo(_: Vec<i32>) {
}
```
Now adds `foo(vec0);` before `vec0.push(88);`
It does not compile anymore. Not there is no delete keyword.
`vec0` is given to `foo` so that is does not belong to `main` after the call to `foo`.
At the end of `foo` the Rust compiler automatically deletes `vec0` at its end since it is not used anymore by this method.
Contrary to C and C++, Rust puts a strong effort on object scoping so that the compiler knows where objects life end.
## Running Rustlings, the Rust tutorials
`~/.cargo/bin/rustlings watch`
Follow the given instructions while editing `rs` files with a text editor.
Focus on code quality:
- `variables4.rs`, what is the difference between mutability and immutability? Which relation with code safety?
- You can skip the exercises on primitive types (by removing `// I AM NOT DONE`) as these are very simple and not related to the course.
- `vecs2.rs`. Rust has pointers as in C++/C. Why the second function (the one that uses `map`) should be used instead of the first one? (still related to mutability)
- `move_semantics1.rs`. As suggested by the *hint* command, why the code does not compile if you put `println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);` after the call to `fill_vec`? For that read: https://doc.rust-lang.org/stable/rust-by-example/scope/move.html On this point Rust strongly differs from C++/C: this is not a copied argument.
- You can skip `move_semantics3.rs`, `move_semantics4.rs`, `move_semantics6.rs`