Skip to content
tezvyn:

Define a WebEvent enum with PageLoad, PageUnload, and KeyPress

Source: doc.rust-lang.orgEasyHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

Given enum WebEvent { PageLoad, PageUnload, KeyPress(char) }, which line correctly instantiates the KeyPress variant with 'q'?

  • a.let press = KeyPress('q');
  • b.let press = WebEvent.KeyPress('q');
  • c.let press = WebEvent::KeyPress { key: 'q' };
  • d.let press = WebEvent::KeyPress('q');Correct
Why?

Rust namespaces enum variants under the type with double colons, so WebEvent::KeyPress('q') is correct. WebEvent.KeyPress('q') is invalid because Rust does not use dot notation for enum variants, unlike some other languages.

Just read this? Test yourself on what you have been reading.

Read the original → doc.rust-lang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on rust — each one lists the topics its interview covers.

See open roles