Crates.io | display_with |
lib.rs | display_with |
version | 0.1.0 |
source | src |
created_at | 2024-09-23 09:48:03.114092 |
updated_at | 2024-09-23 09:48:03.114092 |
description | Return opaque impl Display and/or impl Debug types via display_with and debug_with. |
homepage | |
repository | https://github.com/JohnScience/display_with |
max_upload_size | |
id | 1383804 |
size | 18,383 |
Return opaque impl Display and/or impl Debug types via display_with and debug_with.
cargo add display_with
use display_with::{display_with, debug_with};
let display = display_with(|f| write!(f, "Hello, world!"));
assert_eq!(format!("{display}"), "Hello, world!");
let debug = debug_with(|f| write!(f, "Hello, world!"));
assert_eq!(format!("{debug:?}"), "Hello, world!");
This can be combined with the format_args!
macro to use the opaque types with the write!
and writeln!
macros.
use core::fmt::Write;
use display_with::{display_with, debug_with};
fn main() -> std::fmt::Result {
let display = display_with(|f| write!(f, "Hello, world!"));
let mut s = String::new();
// Unlike `s.push_str(&format!("{display}"))`, this doesn't require an extra allocation.
write!(&mut s, "{}", format_args!("{display}"))?;
Ok(())
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.