Google News
logo
Rust - Interview Questions
What is the difference between a struct and an enum in Rust?
In Rust, both structs and enums are used to define custom data types, but they serve different purposes and have distinct characteristics:

Structs :
* Structure : Structs, short for "structures," allow you to define a named collection of fields that can have different types. They represent a structured data type where each field has a name and a corresponding value.
* Fields: Structs have one or more fields, and you can access and modify these fields individually. Each field can have its own type, and the fields are accessed using dot (`.`) notation.
* Data Storage: Structs store their data on the stack by default. However, they can also store references or smart pointers that allow more complex ownership and borrowing patterns.
* Customization: You can implement methods and associated functions for structs, providing behavior and functionality specific to that struct type.
* Common Use: Structs are commonly used for modeling entities, representing objects, or organizing related data fields.

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

let p = Point { x: 10, y: 20 };
println!("x: {}", p.x);  // Output: x: 10​
Enums :
* Enumeration: Enums, short for "enumerations," allow you to define a type that represents a finite set of possible values. Each value in an enum is called a variant, and each variant can have different associated data or no data at all.
* Variants: Enums can have one or more variants, and each variant can be thought of as a different state or variant of the enum type. Variants can have associated data, allowing for more flexible and expressive data modeling.
* Pattern Matching: Enums are often used with pattern matching to handle different cases or states of the enum. Pattern matching allows you to perform different actions based on the variant and associated data.
* Data Storage: Enums store their data on the stack. The size of the enum is determined by the size of its largest variant.
* Customization: You can implement methods and associated functions for enums, providing behavior and functionality specific to certain enum variants.
* Common Use: Enums are commonly used for modeling state machines, representing options or choices, or handling error conditions.

Example enum definition :
enum Direction {
    Up,
    Down,
    Left,
    Right,
}

let d = Direction::Up;
match d {
    Direction::Up => println!("Moving up"),
    Direction::Down => println!("Moving down"),
    Direction::Left => println!("Moving left"),
    Direction::Right => println!("Moving right"),
}​

Structs are used to define custom data types with named fields, while enums represent a finite set of possible values or states. Structs are useful for organizing related data, while enums provide flexibility for modeling different cases or states and are often used with pattern matching. The choice between structs and enums depends on the nature of the data and the problem you are trying to solve.
Advertisement