Macro dbg_ref

Source
macro_rules! dbg_ref {
    ($val:expr $(,)?) => { ... };
    ($($val:expr),+ $(,)?) => { ... };
}
Expand description

Similar to the dbg! macro, but inspects values by reference without moving them. This allows debugging values without transferring ownership, while showing the underlying value’s type information (not reference types).

This macro uses log::debug! internally, so you must:

  1. Configure a logger (e.g., env_logger) with debug level enabled
  2. Initialize the logger before use

§Key Differences from dbg!

  • Returns () instead of the original value
  • Requires explicit logger initialization

§Examples

Basic usage with automatic type deduction

use testutils::dbg_ref;
// Must initialize logger first:
// env_logger::builder().filter_level(log::LevelFilter::Debug).init();

let counter = 42;
dbg_ref!(counter); // [DEBUG] counter: i32 = 42

let message = "Hello";
dbg_ref!(message); // [DEBUG] message: &str = "Hello"

let values = vec![(1, "a"), (2, "b")];
dbg_ref!(values); // [DEBUG] values: alloc::vec::Vec<(i32, &str)> = [...]

Debugging multiple values

use testutils::dbg_ref;
let width = 30;
let label = "size";

dbg_ref!(width, label);
// Outputs:
// [DEBUG] width: i32 = 30
// [DEBUG] label: &str = "size"

§Implementation Notes

  1. Uses core::any::type_name_of_val for type information
  2. Formats output as: {variable_name}: {type} = {debug_representation}
  3. Multiple arguments generate separate log entries