Define a WebEvent enum with PageLoad, PageUnload, and KeyPress
Tests Rust enum syntax: unit versus tuple variants. A good answer defines WebEvent with PageLoad, PageUnload, and KeyPress(char), then instantiates WebEvent::KeyPress('q'). A red flag is forgetting the double colon or using struct variant syntax.
WHAT THIS TESTS: This question tests your grasp of Rust enum definition and instantiation syntax, specifically the distinction between unit variants that carry no data and tuple variants that hold associated values. It also checks whether you understand namespacing, because Rust enum variants live under the enum identifier using double colon syntax rather than dot notation.
A GOOD ANSWER COVERS: A strong response should do four things in order. First, write the enum definition exactly as enum WebEvent { PageLoad, PageUnload, KeyPress(char) }. Second, clarify that PageLoad and PageUnload are unit variants with no associated data, while KeyPress(char) is a tuple variant that stores a character. Third, instantiate the key press correctly as let press = WebEvent::KeyPress('q'); and note that the variant name is namespaced under WebEvent with double colons. Fourth, optionally mention that each variant acts as a constructor function, so KeyPress('q') constructs a value of type WebEvent.
COMMON WRONG ANSWERS: Interviewers listen for several mistakes. One is using dot syntax like WebEvent.KeyPress('q') instead of double colons, which confuses Rust with other languages. Another is using struct variant syntax like KeyPress { key: 'q' } when the definition specifies a tuple variant. A third red flag is omitting the enum name entirely and writing KeyPress('q') without WebEvent::, which fails because the variant is not in scope by itself unless imported. Finally, some candidates define separate structs for each event and miss the point of using an enum to represent mutually exclusive possibilities.
LIKELY FOLLOW-UPS: If you answer quickly, the interviewer might ask how you would handle this enum in a match expression, which lets you destructure the char out of KeyPress. They might also ask how enums compare to structs for event modeling, or how you would attach different data types to each variant, such as coordinates to PageLoad. Another common follow-up is asking about memory layout: enum variants with data carry a discriminant to tell them apart at runtime.
ONE CONCRETE EXAMPLE: Here is a complete snippet you could write on the board. Define the enum as enum WebEvent { PageLoad, PageUnload, KeyPress(char) }. Then in main, create the variable with let event = WebEvent::KeyPress('q');. If you wanted to react to it, you could write a match block like match event { WebEvent::PageLoad => println!("loaded"), WebEvent::PageUnload => println!("unloaded"), WebEvent::KeyPress(c) => println!("pressed {}", c), }. This shows the enum acting as both a type and a constructor while demonstrating exhaustive pattern matching.
Read the original → doc.rust-lang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.