human_talk_intro_to_rust



human_talk_intro_to_rust

0 0


human_talk_intro_to_rust


On Github Shiroy / human_talk_intro_to_rust

Introduction to Rust

Antoine Wacheux

awacheux.utc@gmail.com

@AntoineWacheux

Github

Features

  • System oriented programming language
  • Prevent segfaults
  • Safe concurrent programming
  • Compiler based on LLVM

Projects

  • Servo (web rendering engines)
  • RedoxOS (operating system)
  • Iron (service oriented web framework)
Put your speaker notes here. You can see them pressing 's'.

Replace C++

C++ is heavily used in the industry. It is efficient and portable.

Recent improvement : lambdas, closure, type inference.

But

  • C++ is 32 years old.
  • Complex language, bug prompt.
  • Difficult to learn when coming from modern languages such as Java, Ruby or Python
C++ rapide mais pas safe (schéma mozilla). Management des ressources à la main. Exemple de WebAudio : (bugs secu critiques due au model mémoire)

Rust is...

Traditional features:

  • imperative programming
  • object oriented programming
  • generics (eg: template)
  • typing

Modern features:

  • closure
  • type inference
  • pattern matching
  • procedural macros
  • compile time memory safety check

Hello world

fn main() {
    println!("Hello l'UTC");
}
Hello l'UTC
Put your speaker notes here. You can see them pressing 's'.

Structures

use std::num::Float;

struct Point {
    x:f64, y:f64
}

fn  main()  {
    let a = Point { x:3.0, y:4.0 };
    let dist = (a.x*a.x + a.y*a.y).sqrt();
    println!("{} {} {}", a.x, a.y, dist);
}
3 4 5
Equivalent aux classes. Pas de classes abstraites.

Structures and methods

use std::f64::consts::PI;
use std::num::Float;

struct Circle { x:f64, y:f64, radius:f64 }
impl Circle {
    fn area(&self) -> f64 { PI*self.radius*self.radius }
}

fn  main()  {
    let c = Circle { x:3.0, y:3.0, radius:2.0 };
    println!("{:?}", c.area());
}
12.566371
Implémentation dans des blocs modulaires. Méthodes statiques.

Traits

trait HasArea { fn area(&self) -> f64; }

struct Circle { x:f64, y:f64, radius:f64 }
impl HasArea for Circle {
    fn area(&self) -> f64 { PI*self.radius*self.radius }
}

struct Rect { x1:f64, y1:f64, x2:f64, y2:f64 }
impl HasArea for Rect {
    fn area(&self) -> f64 { (self.x2-self.x1).abs()*(self.y2-self.y1).abs() }
}

fn  main()  {
    let r = Rect { x1:3.0, y1:3.0, x2:10.0, y2: 7.0};
    let c = Circle { x:3.0, y:3.0, radius:2.0 };
    println!("{:?} {:?}", r.area(), c.area());
}
28 12.566371
Equivalent classes abstraites pures. Permet le polymorphisme et le dispatching dynamique

Pattern matching

enum Resultat {
    Erreur(String),
    Ok(i32)
}

fn diviser(a: i32, b: i32) -> Resultat {
    if b == 0 {
        Erreur(String::from("Division par 0"))
    }
    else {
        Ok(a/b)
    }
}

fn main() {
    match diviser(4, 2) {
        Erreur(msg) => println!("{}", msg),
        Ok(result) => println!("Resultat : {}", result),
    }
}
Resultat : 2
Type pouvant prendre un ensemble de valeur + payload. Exemple Option / Result

And now ?

Lot of topics to cover !

  • ownership and borrowing (next talk)
  • macros
  • the standard library
  • cargo
  • and many mores !
Décrire en 2 phrases cargo.

Conclusion

  • High potential language
  • Hard to learn but extremely powerful once mastered
  • As safe as python and as fast as C

Links & My projects

Official website : https://www.rust-lang.org/

Lot of examples : http://rustbyexample.com/

#LO21 : https://github.com/Shiroy/NPI

#NF11 : https://github.com/Shiroy/lalr_gen

Put your speaker notes here. You can see them pressing 's'.
Introduction to Rust Antoine Wacheux awacheux.utc@gmail.com @AntoineWacheux Github