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

#[derive(Debug)]

enum Person {
   ID(i32),Name(String)
}

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

Output:

ID(100)
Name("Herry Sharma")

Explanation:

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 www.includehelp.com. All rights reserved.