• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【转】rust--如何打印struct实例对象?

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

原文:https://www.jianshu.com/p/0ba974c3eaad

 

 

 

 

 

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)
}

  

----------------------------

 

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)
}

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB R2020B 使用教学——窗口布局设置发布时间:2022-07-18
下一篇:
【转】 MATLAB GUI界面大小 自动适应发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap