zoukankan      html  css  js  c++  java
  • 【转】rust--如何打印struct实例对象?

    原文: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)
    }
    

      

  • 相关阅读:
    selenium-元素无法定位解决办法
    OpenResty 最佳实践
    openresty 学习笔记小结:综合应用实例
    openresty 学习笔记六:使用session库
    openresty 学习笔记番外篇:python的一些扩展库
    openresty 学习笔记番外篇:python访问RabbitMQ消息队列
    openresty 学习笔记五:访问RabbitMQ消息队列
    openresty 学习笔记四:连接mysql和进行相关操作
    openresty 学习笔记三:连接redis和进行相关操作
    Grunt教程——初涉Grunt
  • 原文地址:https://www.cnblogs.com/oxspirt/p/15408289.html
Copyright © 2011-2022 走看看