• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

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

开发技术 开发技术 10小时前 3次浏览

 

 

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

 

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

 

 

 

 

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

  


程序员灯塔
转载请注明原文链接:【转】rust–如何打印struct实例对象?
喜欢 (0)