-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
405bb43
commit b05dc7c
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# What is the differences between Structs vs Enums? | ||
|
||
Enums and structs are both ways to define custom data types in Rust, but they serve different purposes and have different characteristics. Here’s a detailed comparison of enums and structs: | ||
|
||
[**Structs**](./struct.md) | ||
|
||
Structs are used to group related data together. They are similar to classes in object-oriented languages but without methods for encapsulation. Structs are typically used to represent entities with a fixed set of attributes. | ||
|
||
**Characteristics of Structs** | ||
|
||
**Fixed Set of Fields**: Structs have a fixed set of fields, each with a name and a type. | ||
|
||
**Homogeneous Data**: All instances of a struct have the same fields. | ||
|
||
**Data Grouping**: Structs are used to group related data together. | ||
|
||
[**Enums**](./enum.md) | ||
|
||
Enums are used to define a type that can be one of several different variants. Each variant can have associated data. Enums are typically used to represent a value that could be one of several different types. | ||
|
||
**Characteristics of Enums** | ||
|
||
**Multiple Variants**: Enums can have multiple variants, each representing a different possible value. | ||
|
||
**Heterogeneous Data**: Each variant of an enum can have different types and amounts of associated data. | ||
|
||
**Pattern Matching**: Enums are often used with pattern matching to handle different cases. |