Hone logo
Hone
Problems

Implementing Custom Assert Macros in Rust

Rust's built-in assert! macro is a powerful tool for debugging and ensuring code correctness during development. This challenge asks you to implement your own simplified versions of assert! and assert_eq!, providing a deeper understanding of macro expansion and conditional compilation in Rust. Successfully completing this challenge will solidify your grasp of Rust's macro system and its role in compile-time checks.

Problem Description

You are tasked with creating two macros: my_assert! and my_assert_eq!.

  • my_assert!(condition): This macro should behave identically to Rust's assert! macro. If condition evaluates to false, the macro should cause the program to panic with a message indicating the assertion failed. If condition is true, the macro should expand to nothing (effectively being removed during compilation).

  • my_assert_eq!(left, right): This macro should behave like Rust's assert_eq! macro. It should compare left and right for equality. If they are not equal, the macro should panic with a message indicating the assertion failed, including the values of left and right. If they are equal, the macro should expand to nothing.

Key Requirements:

  • The macros must compile without warnings.
  • The panic messages should be informative, clearly indicating which assertion failed and, in the case of my_assert_eq!, the values being compared.
  • The macros should expand to nothing when the condition is true.
  • The macros should use the panic! macro for error reporting.

Examples

Example 1:

Input:
```rust
my_assert!(1 == 1);

Output:

(No output - the macro expands to nothing)
Explanation: The condition `1 == 1` is true, so the macro does nothing.

Example 2:

Input:
```rust
my_assert!(1 == 2);

Output:

thread 'main' panicked at 'assertion failed: 1 == 2', src/main.rs:2:5
note: run with `RUST_BACKTRACE=1` to display a backtrace

Explanation: The condition 1 == 2 is false, so the macro panics with the specified message.

Example 3:

Input:
```rust
my_assert_eq!(5, 5);

Output:

(No output - the macro expands to nothing)
Explanation: The values 5 and 5 are equal, so the macro does nothing.

Example 4:

Input:
```rust
my_assert_eq!("hello", "world");

Output:

thread 'main' panicked at 'assertion failed: "hello" == "world"', src/main.rs:2:5
note: run with `RUST_BACKTRACE=1` to display a backtrace

Explanation: The strings "hello" and "world" are not equal, so the macro panics with the specified message.

Constraints

  • The macros must be implemented using Rust's macro system.
  • The panic messages should be clear and concise.
  • The code should be well-formatted and readable.
  • The solution should not rely on external crates.

Notes

  • Consider using the stringify! macro to create the panic message dynamically.
  • Think about how to handle different data types within the my_assert_eq! macro. While a fully generic solution is beyond the scope of this exercise, ensure it works for basic types like integers and strings.
  • The goal is to understand the basic principles of macro implementation, not to create a production-ready assertion library. Focus on clarity and correctness.
  • Remember that macros are expanded at compile time.
Loading editor...
rust