Hone logo
Hone
Problems

Implementing a Unit Struct in Rust

Unit structs in Rust are a powerful, yet often overlooked, feature. They represent data types that have no meaningful data members, serving primarily as markers or tags. This challenge will guide you through creating and utilizing a unit struct, demonstrating its utility in scenarios where you need a distinct type without associated data.

Problem Description

Your task is to define a unit struct named MyMarker. This struct should have no fields. Then, create a function process_marker that takes a MyMarker as input and returns a string "Marker processed!". The purpose of this exercise is to understand how to define and use a unit struct, showcasing its ability to represent a type without any data. Unit structs are useful for things like representing states, events, or simply distinguishing between different code paths.

Examples

Example 1:

Input: MyMarker {}
Output: "Marker processed!"
Explanation: The function receives an instance of the unit struct `MyMarker` and returns the specified string.

Example 2:

Input: let marker = MyMarker{};
Output: "Marker processed!" (after calling process_marker(marker))
Explanation:  A `MyMarker` instance is created and passed to the `process_marker` function, which then returns the expected string.

Constraints

  • The struct must be named MyMarker.
  • The function must be named process_marker.
  • The function must accept a MyMarker as input.
  • The function must return a String.
  • The code must compile and run without errors.

Notes

  • Unit structs are defined using empty curly braces {} after the struct name.
  • Because unit structs have no fields, they are always sized (unlike tuples).
  • Consider how a unit struct can be useful even though it doesn't hold any data. Think about scenarios where you need a distinct type to represent a specific state or event. The absence of data is the key characteristic.
Loading editor...
rust