Rust



Rust

0 0


fr-rust-presentation

Rust presentation for Brussels based engineering school ECAM

On Github azerupi / fr-rust-presentation

Rust

Par Mathieu David / @azerupi

Un de plus...

Rust ...

  • guarantit la sécurité de la mémoire
  • empèche les data races
  • offre une performance ...
  • offre un accès de bas niveau ...

... comparable au C / C++

Exemple C++

	#include <iostream>
	#include <vector>

	int main() {
	    std::vector<int> v;
	    v.push_back(1);

	    int& elem = v[0];

	    for(int i=2; i<=100; i++)
	        v.push_back(i);

	    std::cout << elem << std::endl;

	    return 0;
	}
					
  • Aliasing
  • Mutation
  • Aliasing + Mutation

Équivalent en Rust

fn main() {
    let mut v = Vec::new();
    v.push(1);

    let elem = &v[0];

    for i in 2..101 {
        v.push(i);
    }

    println!("{}", elem);
}
					

Ownership & Borrowing

Propriétaire:

  • Unique
  • Responsable de la mémoire
fn exemple() {
	let mut s = String::from("Hello World!");

} // <-- s sort de sa portée, la mémoire est désallouée
						

Ownership & Borrowing

Emprunt:

  • Immutable
    • Autant qu'on veut
    • Propriétaire devient temporairement immutable
  • Mutable
    • UN SEUL
    • Propriétaire devient temporairement inaccéssible
:9:9: 9:10 error: cannot borrow `v` as mutable because it is also borrowed as immutable [E0502]
:9         v.push(i);
           ^
:6:17: 6:18 note: previous borrow of `v` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `v` until the borrow ends
:6     let elem = &v[0];
                  ^
:13:2: 13:2 note: previous borrow ends here
:2 fn main() {
...
:13 }
    ^
error: aborting due to previous error
					
Rust Par Mathieu David / @azerupi