[rust整理笔记]rust操作小技巧

#![allow(dead_code)]
#[derive(Debug)]
struct Soo{
    x:i32,
    y:i32
}

fn main() {
// 1.print 调试
    let s0 = Soo{x:12,y:30};
//   通过 .. 操作符后面跟着这个结构体的另一个实例
   let s = Soo{x:12,..s0};
//    打印结构体
    println!("{:?}",s);
//    格式化输出
    println!("{:#?}",s);
//    注意:打印结构体前面要加#[derive(Debug)]

//    2.填充格式操作符
    let score1 = 100;
    let score2 = 1000;
    let score3 = 10000;

    println!("{:>5}", score1);
    println!("{:>5}", score2);
    println!("{:>5}", score3);
//    如果你想要两边填充,也有办法, :^ 后面跟上填充后的字符串宽度就行了
    let padded = "padded";
    println!("[{:^10}]", padded);
//    还可以用不同的字符进行填充,只要在箭头前面指定填充字符就行了
    let title = "SCORES";
    let player1 = "first player:";
    let player2 = "second player:";
    let player3 = "third player:";
    let score1 = 100;
    let score2 = 1000;
    let score3 = 10000;

    println!("{:_^20}", title);
    println!("{:<14} {:>5}", player1, score1);
    println!("{:<14} {:>5}", player2, score2);
    println!("{:<14} {:>5}", player3, score3);
//    原文地址:https://saghm.github.io/five-rust-things/

}

原文地址:原文地址:https://saghm.github.io/five-rust-things/
资源下载
资源下载
[rust整理笔记]rust操作小技巧_第1张图片

你可能感兴趣的:(7.Rust知识-应用笔记)