在C++中,我使用类似于DEBUG
宏的东西:
#ifdef DEBUG
#define DEBUG_STDERR(x) (std::cerr << (x))
#define DEBUG_STDOUT(x) (std::cout << (x))
#else
#define DEBUG_STDERR(x)
#define DEBUG_STDOUT(x)
#endif
rust 有类似的东西吗?
在C++中,我使用类似于DEBUG
宏的东西:
#ifdef DEBUG
#define DEBUG_STDERR(x) (std::cerr << (x))
#define DEBUG_STDOUT(x) (std::cout << (x))
#else
#define DEBUG_STDERR(x)
#define DEBUG_STDOUT(x)
#endif
rust 有类似的东西吗?
虽然像DK的回答中提到的那样使用log
crate 是有意义的,但以下是如何直接完成你所要求的:
// The debug version
#[cfg(feature = "my_debug")]
macro_rules! debug_print {
($( $args:expr ),*) => { println!( $( $args ),* ); }
}
// Non-debug version
#[cfg(not(feature = "my_debug"))]
macro_rules! debug_print {
($( $args:expr ),*) => {}
}
fn main() {
debug_print!("Debug only {}", 123);
}
在你的Cargo.toml
中,添加[features]
部分:
[features]
my_debug = []
然后,输出显示为cargo run --features my_debug
,而不是普通的cargo run
.