← Back to Rust Course | Chapter 8: Enums & Pattern Matching | Lesson 1 of 7

Enum के Basics

एक enum किसी value को पहले से define कई named choices में से बिल्कुल एक होने देता है।
Syntax
rust
enum Name {
    Variant1,
    Variant2(Type),
    Variant3 { field: Type },
}

let value = Name::Variant1;

Defining a Simple Enum

एक enum एक type define करता है जिसकी value named variants के एक fixed set में से बिल्कुल एक हो सकती है, possibilities के एक closed set को represent करने के लिए उपयोगी।

उदाहरण: Defining a Simple Enum

markup
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define an enum `Direction` listing its possible variants
enum Direction {
    North,
    South,
    East,
    West,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `heading`, set to `Direction::North`
    let heading = Direction::North;
    // Print "{:?}" to the console, substituting in `heading` (with a trailing newline)
    println!("{:?}", heading);
}

Enums With Data

Simple labels के विपरीत, Rust enum variants अपना खुद का associated data रख सकते हैं, और अलग-अलग variants अलग-अलग types और मात्रा में data रख सकते हैं।

उदाहरण: Enums With Data

markup
// Auto-generate #[derive(Debug)] for the type below
#[derive(Debug)]
// Define an enum `Shape` listing its possible variants
enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `shapes`, set to `[Shape::Circle(2.0), Shape::Rectangle(3.0, 4.0)]`
    let shapes = [Shape::Circle(2.0), Shape::Rectangle(3.0, 4.0)];
    // Loop over `shapes.iter()`, binding each item to `s`
    for s in shapes.iter() {
        // Print "{:?}" to the console, substituting in `s` (with a trailing newline)
        println!("{:?}", s);
    }
}

Matching on an Enum

यह जांचने का primary तरीका match है कि एक enum value किस variant को रखता है और इसके रखे किसी भी data को निकालने के लिए, हर possible variant के लिए एक अलग arm के साथ।

उदाहरण: Matching on an Enum

markup
// Define an enum `TrafficLight` listing its possible variants
enum TrafficLight {
    Red,
    Yellow,
    Green,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `light`, set to `TrafficLight::Yellow`
    let light = TrafficLight::Yellow;
    // Match on `light` and branch by pattern
    match light {
        TrafficLight::Red => println!("Stop"),
        TrafficLight::Yellow => println!("Slow down"),
        TrafficLight::Green => println!("Go"),
    }
}

Comparing Enum Values

किसी enum पर PartialEq derive करना इसकी values को सीधे == से compare करने देता है, जो एक पूरा match लिखे बिना साधारण checks के लिए उपयोगी है।

उदाहरण: Comparing Enum Values

markup
// Auto-generate #[derive(PartialEq, Debug)] for the type below
#[derive(PartialEq, Debug)]
// Define an enum `Status` listing its possible variants
enum Status {
    Active,
    Inactive,
}

// Entry point: execution of the program starts here
fn main() {
    // Declare an immutable variable `current`, set to `Status::Active`
    let current = Status::Active;
    // Print "Is active: {}" to the console, substituting in `current == Status::Active` (with a trailing newline)
    println!("Is active: {}", current == Status::Active);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि एक enum variant अपना खुद का data रख सकता है, सिर्फ कुछ दूसरी languages की तरह एक plain label नहीं।
  2. enum पर PartialEq derive करने से पहले variants को == से compare करने की कोशिश करना।
  3. यह मान लेना कि enum variant names globally unique हैं -- import किए बिना इन्हें EnumName::Variant से qualify किया जाना चाहिए।
चैप्टर सारांश
  • एक enum एक type define करता है जो कई named variants में से बिल्कुल एक हो सकता है।
  • Variants optionally associated data रख सकते हैं, हर variant के लिए अलग data सहित।
  • Enum values को आमतौर पर यह branch करने के लिए match से handle किया जाता है कि कौन सा variant मौजूद है।
  • किसी enum पर Debug और PartialEq जैसे traits derive करना printing और comparison enable करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.