走进 Rust:打印结构体字段
Rust 大约 1017 字定义结构体
struct Rectangle {
width: u32,
height: u32,
}
打印结构体
使用{}
打印会报错。
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
println!("rect1 is {}", rect1);
}
提示信息:
= help: the trait `std::fmt::Display` is not implemented for `Rectangle`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
使用{:?}
或{:#?}
打印结构体中的字段信息。
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
println!("rect1 is {:?}", rect1);
}
提示信息:
= help: the trait `std::fmt::Debug` is not implemented for `Rectangle`
= note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
结构体上增加#[derive(Debug)]
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
println!("rect1 print by :?, {:?}", rect1);
println!("rect1 print by :#?, {:#?}", rect1);
}
输出:
rect1 print by :?, Rectangle { width: 30, height: 50 }
rect1 print by :#?, Rectangle {
width: 30,
height: 50,
}
阅读 5992 · 发布于 2020-07-09
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
软考-系统架构设计师:范围管理和时间管理阅读 1751
-
MySQL 合并字符串函数:CONCAT、CONCAT_WS、GROUP_CONCAT阅读 3845
-
软考-系统架构设计师:规范化理论-函数依赖阅读 1355
-
Arthas 使用 stack 查找指定方法是被谁调用的阅读 2815
-
javac 错误: 编码 GBK 的不可映射字符阅读 1241
-
Lombok MapStruct cannot find symbol阅读 1084
-
OpenResty 整合 LuaRocks - Windows10阅读 3150
-
Kubernetes Pod 控制器 DaemonSet阅读 898
-
filebeat read: connection reset by peer阅读 3436
-
Nginx 配置静态文件 404(root 与 alias 区别)阅读 5059