- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码:
#[derive(Debug)]
struct Test {
int: u8,
bits: u8,
hex: u8
}
fn main() {
let t = Test {
int: 1,
bits: 2,
hex: 3
};
println!("{:#?}", t);
}
运行时输出如下:
Test {
int: 1,
bits: 2,
hex: 3
}
很酷,我们可以毫不费力地转储结构,但我的一些数据结构包含位掩码或其他不易以 10 为基数读取的数据。对于位字段,读取它会容易得多,它们是输出类似 0b00000010
.
有没有一种简单的方法来获得这样的输出,而无需实现 Debug
直接在结构上的特征(在另一种类型上的 Debug
特征会很好)?
Test {
int: 1,
bits: 0b00000010,
hex: 0x03
}
如果必须的话,我可以使用不同的类型,但我希望保持结构本身的调试特性像 #[derive(Debug)]
一样简单。 .
最佳答案
Is there a simple way to get output like this, without implementing the
Debug
trait directly on the struct (aDebug
trait on another type would be fine)
是的。 #[derive(Debug)]
通过依次调用每个成员的 Debug::debug
来实现 Debug
。按照 How to implement a custom 'fmt::Debug' trait? 中的说明进行操作对于您的新型包装器:
use std::fmt;
#[derive(Debug)]
struct Test {
int: u8,
bits: Bits,
hex: u8,
}
struct Bits(u8);
impl fmt::Debug for Bits {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0b{:08b}", self.0)
}
}
在不同的方向,在你的主结构上有一个方法返回另一个“用于显示目的”的结构,类似于 std::path::Display
并不奇怪.这允许您将复杂的显示逻辑移动到一个单独的类型,同时允许您的原始结构没有可能妨碍您的新类型:
use std::fmt;
#[derive(Debug)]
struct Test {
int: u8,
bits: u8,
hex: u8,
}
impl Test {
fn pretty_debug(&self) -> PrettyDebug<'_> {
PrettyDebug {
int: &self.int,
bits: Bits(&self.bits),
hex: &self.hex,
}
}
}
#[derive(Debug)]
struct PrettyDebug<'a> {
int: &'a u8,
bits: Bits<'a>,
hex: &'a u8,
}
struct Bits<'a>(&'a u8);
impl fmt::Debug for Bits<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0b{:08b}", self.0)
}
}
引用 u8
有点傻,但引用是这里最通用的解决方案 — 选择适合您的情况的数据类型。
您还可以直接为您的 PrettyDebug
类型实现 Debug
:
use std::fmt;
#[derive(Debug)]
struct Test {
int: u8,
bits: u8,
hex: u8,
}
impl Test {
fn pretty_debug(&self) -> PrettyDebug<'_> {
PrettyDebug(self)
}
}
struct PrettyDebug<'a>(&'a Test);
impl fmt::Debug for PrettyDebug<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Test")
.field("int", &self.0.int)
.field("bits", &format_args!("0b{:08b}", self.0.bits))
.field("hex", &format_args!("0x{:02x}", self.0.hex))
.finish()
}
}
关于rust - 有没有一种方法可以在不直接实现 Debug 特性的情况下自定义 Debug 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46749679/
目录 C#特性 1. 概括 2. 语法 定义特性类 应用特性
是否有关于 Python 语言必须提供哪些功能的文章/论文?为什么应该使用 Python 而不是任何其他语言? Python 的优点和缺点是什么? 最佳答案 Why Python和 Why Pytho
我想弄清楚为什么 .x比*.x具有更高的特异性当后者有望获胜时。 不是*.x应该具有 0-0-1-1 的特异性(1 个类,1 个标签)而 .x只是一个类(class) 0-0-1-0 ? 考虑以下基本
在尝试评估非 instanceof 条件时,我发现了我在 Groovy 2.4.7、1.6.0 JVM 中没有预料到的行为。 总之: class Foo { static Boolean
当使用可变结构和属性时,编译器可以解决一些问题,但不能对其他相当明显的事情做同样的事情,这对我来说似乎很奇怪。 以下面的自动属性为例: Vector2 Vector { get; set; } 而不是
我对 ES3 有一定的了解,但我对 ES5 的特性不是很了解。我 - 或多或少 - 知道: Object.create(), Object.freeze() “使用严格” getter 和 sette
我最近开始将 Django 1.5.4 用于带有 MySQL 后端的 Web 应用程序。就在一开始,我遇到了某些限制,这让我想知道 Django 是否是继续前进的正确方法。 一些明显的缺点是: 缺少复
在函数模板的定义中,模板参数的实例化一般是未知的。类型特征可用于在编译时获取一些信息。例如,这是 is_pointer 的一个简单应用: template void foo(T p) { cout
我正在设计页面的样式,该页面具有除最后一个框外的带底部边框的连续框。我为所有框应用类 .box 并添加 .box_last 以仅隐藏最后一个框的边框。 .box { border-bottom-s
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我有兴趣编制一份不建议在嵌入式系统中使用的 c++ 功能列表(这可能会导致人们推荐使用 c 而不是 c++)。如果您知道,请尝试添加原因,或者将您的原因添加到其他人的答案中。 这是一个开始(我知道的唯
作为一名 Ruby 程序员,您是否曾觉得任何使用起来有点冒险的功能,可能是因为它的奇怪行为?它可能有很好的文档记录,但在调试时很难找到,或者难以记住? 我通常尽量远离 String#gsub!。文档说
我正在开发一个连接到健身 watch 的蓝牙应用程序。这是我第一次使用蓝牙。我设法使用出色的 FlutterBlue 将我的应用程序与设备连接起来图书馆。 但是我无法理解我从阅读中获得的结果。这是我阅
1. 迭代器(Iterator)的介绍 背景:指针可以用来遍历存储空间连续的数据结构,但是对于存储空间费连续的,就需要寻找一个行为类似指针的类,来对非数组的数据结构进行遍历。 定义:迭代器是一种检
嗨,伙计们,有没有动态更改 Spring 属性文件内容的好例子?如果您能给我一些示例或链接,我将不胜感激。 非常感谢 最佳答案 我想你可以使用 ReloadableResourceBundleMess
SystemVerilog 引入了一些非常有用的结构来改进编码风格。然而,正如我的一位同事经常说的,“你不是在写软件,你是在描述硬件。”考虑到这一点,当最终结果需要合成时,应该避免语言的哪些特征?这个
我定义了这些测试依赖项 / Test Dependencies lazy val wiremock = "com.github.tomakehurst" % "wir
我正在为 Android 手机和 Android watch (wearOS) 编写应用程序。这些应用程序将通过蓝牙相互通信。基本上,Android 手机上的应用程序将与 WearOS 设备绑定(bi
我正在为 Android 手机和 Android watch (wearOS) 编写应用程序。这些应用程序将通过蓝牙相互通信。基本上,Android 手机上的应用程序将与 WearOS 设备绑定(bi
我有兴趣提高我的设计能力(设计具有属性、方法等的类)。即如何决定类、方法和属性应该是什么? 你们能建议我改进这个的好 Material 吗? 最佳答案 请看: Any source of good o
我是一名优秀的程序员,十分优秀!