Q:

Rust program to create an enum with the data type

belongs to collection: Rust Enums Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 IDName 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

total answers (1)

<< Rust program to demonstrate the Match Statement wi...