@@ -8,12 +8,18 @@ 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, compiles to native code.
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.
Rust may make C++ and C deprecated during the coming decade(s).
## Installation
Rust installer: https://rustup.rs
...
...
@@ -32,6 +38,36 @@ 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
fnmain(){
letmutvec0: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);
}
fnfoo(_: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
...
...
@@ -52,6 +88,7 @@ Focus on code quality:
- 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`