ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.