Les structures en Rust

Des structures tuple

#![allow(unused)]
fn main() {
struct EntierASelectionner(i32, bool);
let i = EntierASelectionner(5, false);
}

Des structures classiques

#![allow(unused)]
fn main() {
struct Point {
    x: i32,
    y: i32
}

let p = Point {x: 5, y: 2};
}

Accès aux champs

#![allow(unused)]
fn main() {
struct Point {
    x: i32,
    y: i32
}
let p = Point {x: 5, y: 2};
println!("{}", p.x);
}

Méthodes

#![allow(unused)]
fn main() {
struct Point {
    x: i32,
    y: i32
}


impl Point {
    fn unitX() -> Point {
        return Point(1, 0)
    }
    fn norm2(&self) -> i32 {
        return self.x ** 2 + self.y ** 2;
    }
}

let u = Point::unitX();
let p = Point {x: 5, y: 2};
println!("{}", p.norm2());
}