0% found this document useful (0 votes)
5 views3 pages

Rust Ko

Uploaded by

dhirendra lamsal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Rust Ko

Uploaded by

dhirendra lamsal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Using the code snippet below, define the different variants utilized

#[derive(Debug)]

enum Message {

// TODO: define the different variants used below

impl Message {

fn call(&self) {

println!("{:?}", &self);

fn main() {

let messages = [

Message::Move { x: 10, y: 30 },

Message::Echo(String::from("hello world")),

Message::ChangeColor(200, 255, 255),

Message::Quit,

];

for message in &messages {

message.call();

Answer text Question 1


Enums are a way of defining custom data types in a different way than you
do with structs. Create your own enum code snippet using a real world
concept that has variants (similar to the IP address type example in the
textbook)

Answer text Question 2

Using the code snippet below, define the message types where applicable

#[derive(Debug)]

enum Message {

// enter your code here

fn main() {

println!("{:?}", Message::Quit);

println!("{:?}", Message::Echo);

println!("{:?}", Message::Move);

println!("{:?}", Message::ChangeColor);

To declare an enumeration, we write the enum keyword, followed by a unique


name and a code block. Inside the code block we declare our actual words
that will map to numbers, separated by commas. Each of these variants can
have an optional type associated to them.

Build a custom enum that has a minimum of 3 variants. Include at least 2


optional types and then initialize these enum variants with relevant data.

The option enum is a predefined generic enum in Rust and allows the enum
to return a value. Because of that, the option enum is a generic, which
means it has a placeholder for a type.
Write a code snippet that builds a custom enum that utilizes the Option<T>
generic and compiles successfully.

Answer text Question 5

You might also like