Skip to content

Latest commit

 

History

History
94 lines (74 loc) · 2.97 KB

File metadata and controls

94 lines (74 loc) · 2.97 KB

Instanciation de Structs - C'est Super Facile! 🎯

Learning Objectives

  • Créer des instances avec struct update syntax (c'est pratique!)
  • Utiliser field init shorthand (raccourci cool!)
  • Comprendre les structs unitaires et tuple structs

Core Explanation

For Absolute Beginners - C'est Comme Remplir une Boîte! 📦

Imaginez une boîte 📦 que vous remplissez:

  • Struct update = Copier une boîte et changer quelques choses
  • Field init shorthand = Raccourci pour éviter de répéter
  • C'est super pratique et super rapide!

Schéma Visuel - Instanciation

┌─────────────────────────────────────────┐
│  📦 INSTANCIATION = REMPLIR BOÎTE 📦   │
├─────────────────────────────────────────┤
│                                         │
│  user1 (boîte remplie)                 │
│  ┌─────────────┐                        │
│  │ username    │                        │
│  │ email       │                        │
│  │ ...         │                        │
│  └─────────────┘                        │
│         │                                │
│         ▼ ..user1 (copie)               │
│  user2 (nouvelle boîte)                 │
│  ┌─────────────┐                        │
│  │ email (new) │                        │
│  │ ... (copié) │                        │
│  └─────────────┘                        │
│                                         │
└─────────────────────────────────────────┘

Mnémonique: "Boîte" - Instancier une struct, c'est remplir une boîte, et struct update copie une boîte en changeant quelques choses!

Code Examples

Example 1: Struct Update Syntax

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

fn main() {
    let user1 = User {
        email: String::from("alice@example.com"),
        username: String::from("alice"),
        active: true,
        sign_in_count: 1,
    };
    
    // Créer user2 en copiant user1 avec modifications
    let user2 = User {
        email: String::from("bob@example.com"),
        username: String::from("bob"),
        ..user1  // Copie les autres champs de user1
    };
}

Example 2: Field Init Shorthand

struct Point {
    x: i32,
    y: i32,
}

fn creer_point(x: i32, y: i32) -> Point {
    Point { x, y }  // Shorthand: x: x devient juste x
}

fn main() {
    let p = creer_point(5, 10);
    println!("Point: ({}, {})", p.x, p.y);
}

Official Resources