Skip to content

Commit

Permalink
enum vs structs
Browse files Browse the repository at this point in the history
  • Loading branch information
saltukalakus committed Nov 1, 2024
1 parent 405bb43 commit b05dc7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [Imp Keyword](./essentials/imp.md)
- [Structs](./essentials/struct.md)
- [Enums](./essentials/enum.md)
- [Structs vs Enums](./essentials/struct-vs-enums.md)

- [Patterns](./patterns.md)
- [Behavioral Patterns](./patterns/behavioral.md)
Expand Down
27 changes: 27 additions & 0 deletions src/essentials/struct-vs-enums.md
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.

0 comments on commit b05dc7c

Please sign in to comment.