Rust program to create an enum with the data type

Rust | Enum Example: Write a program to create an enum with the data type.
Submitted by Nidhi, on October 28, 2021

Problem Solution:

In this program, we will create an enum Person with the data type.  Then we will initialize the enum constants and print their values.

Program/Source Code:

The source code to create an enum with data type is given below. The given program is compiled and executed successfully.

// Rust program to create enum 
// with data type


enum Person {

fn main() {
    let p1 = Person::ID(100);
    let p2 = Person::Name(String::from("Herry Sharma"));


Name("Herry Sharma")


In the above program, we created an enum Person and function main(). The enum Person contains constants ID, Name with the data type.

In the main() function, we initialized the enum constants and printed them on the console screen.

Rust Enums Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.