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.
Program/Source Code:
The source code to create an enum with data type is given below. The given program is compiled and executed successfully.
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer