Aryan PrajapatKnowledge Contributor
What is the difference between a Rust enum and struct?
What is the difference between a Rust enum and struct?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
While both enum and struct provide ways to encapsulate data, they do so in different ways.
A struct contains fields and every field in the struct is present at all times. This makes struct appropriate when you need to group data together and have access to all components of that data.
An enum contains variants in which a single variant gets represented at a time.
This makes enum appropriate when you have more than one data component, but only want a single component at a time.
A parser is an example where using an enum makes sense because a token may be one of a predefined number of items.