如何打印结构体和数组? [英] How to print structs and arrays?

查看:48
本文介绍了如何打印结构体和数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Go 似乎可以直接打印结构体和数组.

Go seems to be able to print structs and arrays directly.

struct MyStruct {
    a: i32,
    b: i32
}

let arr: [i32; 10] = [1; 10];

推荐答案

您想在结构上实现 Debug 特性.使用 #[derive(Debug)] 是最简单的解决方案.然后你可以用 {:?}:

You want to implement the Debug trait on your struct. Using #[derive(Debug)] is the easiest solution. Then you can print it with {:?}:

#[derive(Debug)]
struct MyStruct{
    a: i32,
    b: i32
}

fn main() {
    let x = MyStruct{ a: 10, b: 20 };
    println!("{:?}", x);
}

这篇关于如何打印结构体和数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆