【转】rust--如何打印struct实例对象?
原文:https://www.jianshu.com/p/0ba974c3eaad
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | fn example1() { // 第一种方法是给Struct增加一个derive(Debug). #[derive(Debug)] struct MyStruct {x: i32, y: i32} let ms = MyStruct {x: 0, y: 10}; println!( "{:?}" , ms) } fn example2() { // 第二种方法是自己去实现一个Display. // Refer: https://stackoverflow.com/questions/30253422/how-to-print-structs-and-arrays struct MyStruct {a: i32, b: i32} impl std::fmt::Display for MyStruct { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "(value a: {}, value b: {})" , self.a, self.b) } } let ms = MyStruct { a: 0, b: 15 }; println!( "Used Display: {}" , ms); } fn main() { example1(); // output: MyStruct { x: 0, y: 10 } example2(); // output: Used Display: (value a: 0, value b: 15) } |
----------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | fn main() { println!( "Hello, world!" ); let mut x = 5; x += 2; println!( "{}" , x); println!( "{} days" , 31); // Without a suffix, 31 becomes an i32. You can change what type 31 is // by providing a suffix. The number 31i64 for example has the type i64. // There are various optional patterns this works with. Positional // arguments can be used. println!( "{0}, this is {1}. {1}, this is {0}" , "Alice" , "Bob" ); // As can named arguments. println!( "{subject} {verb} {object}" , object = "the lazy dog" , subject = "the quick brown fox" , verb = "jumps over" ); // Special formatting can be specified after a `:`. println!( "{} of {:b} people know binary, the other half doesn't" , 1, 2 ); // You can right-align text with a specified width. This will output // " 1". 5 white spaces and a "1". println!( "{number:>width$}" , number = 1, width = 6); // You can pad numbers with extra zeroes. This will output "000001". println!( "{number:0>width$}" , number = 1, width = 6); // Rust even checks to make sure the correct number of arguments are // used. println!( "My name is {0}, {1} {0}" , "Bond" , "jjj" ); // FIXME ^ Add the missing argument: "James" // Create a structure named `Structure` which contains an `i32`. // #[allow(dead_code)] // #[derive(Debug)] struct MStructure(i32); impl std::fmt::Display for MStructure { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "(value a: {})" , self.0) } } // // However, custom types such as this structure require more complicated // // handling. This will not work. // // println!("This struct `{}` won't print...", Structure(3)); // // FIXME ^ Comment out this line. let ms = MStructure(3); println!( "{}" , ms); // #[derive(Debug)] // struct MyStruct { // x: i32, // y: i32, // } // let ms = MyStruct { x: 0, y: 10 }; // println!("{:?}", ms) // #[derive(Debug)] // struct MyyStruct(i32); // let mm = MyyStruct(33); // println!("{:?}", mm) } |
标签:
rust
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2020-10-14 go 语言大括号,开启一个新的作用域
2020-10-14 [转]web请求执行流程--这段写的不错