rust:print

println! 打印和调试

  • 打印 {}
  • 调试 {:?} {:#?}
  • {:#?} is called “pretty printing”. It is like {:?} but prints with different formatting over more lines.
  • 指针 {:p}
  • 指定进制 Binary: {:b}, hexadecimal: {:x}, octal: {😮}
  • 指定顺序 {number}
  • 指定pending {: pendingsting ^<> min }

如果用{} 下面代码会编译错误,因为()没有实现display {:?}可以正常输出

fn main() {
    let doesnt_print = ();
    println!("This will not print: {:#?}", doesnt_print); // ⚠️
}

Sometimes you have too many " and escape characters, and want Rust to ignore everything. To do this, you can add r# to the beginning and # to the end.

fn main() {
    println!("He said, \"You can find the file at c:\\files\\my_documents\\file.txt.\" Then I found the file."); // We used \ five times here
    println!(r#"He said, "You can find the file at c:\files\my_documents\file.txt." Then I found the file."#)
}
---
He said, "You can find the file at c:\files\my_documents\file.txt." Then I found the file.
He said, "You can find the file at c:\files\my_documents\file.txt." Then I found the file.

f you need to print with a # inside, then you can start with r## and end with ##. And if you need more than one, you can add one more # on each side.

fn main() {

    let my_string = "'Ice to see you,' he said."; // single quotes
    let quote_string = r#""Ice to see you," he said."#; // double quotes
    let hashtag_string = r##"The hashtag #IceToSeeYou had become very popular."##; // Has one # so we need at least ##
    let many_hashtags = r####""You don't have to type ### to use a hashtag. You can just use #.""####; // Has three ### so we need at least ####

    println!("{}\n{}\n{}\n{}\n", my_string, quote_string, hashtag_string, many_hashtags);

}

---
'Ice to see you,' he said.
"Ice to see you," he said.

The hashtag #IceToSeeYou had become very popular.
“You don’t have to type ### to use a hashtag. You can just use #.”

让关键字做变量,这是什么鬼设计
r# has another use: with it you can use a keyword (words like let, fn, etc.) as a variable name.

fn main() {
    let r#let = 6; // The variable's name is let
    let mut r#mut = 10; // This variable's name is mut
}

为了解决历史遗留问题,开始不是关键字,到后面变成了关键字

r# has this function because older versions of Rust had fewer keywords than Rust now. So with r# you can avoid mistakes with variable names that were not keywords before.
Or maybe for some reason you really need a function to have a name like return. Then you can write this:

fn r#return() -> u8 {
    println!("Here is your number.");
    8
}

fn main() {
    let my_number = r#return();
    println!("{}", my_number);
}

打印字节码,只对ASCII字符有效,否则报错 error: byte constant must be ASCII. Use a \xHH escape for a non-ASCII byte

fn main() {
    println!("{:?}", b"This will look like numbers");
}

---
[84, 104, 105, 115, 32, 119, 105, 108, 108, 32, 108, 111, 111, 107, 32, 108, 105, 107, 101, 32, 110, 117, 109, 98, 101, 114, 115]

b和r#结合使用

fn main() {
    println!("{:?}", br##"I like to write "#"."##);
}

There is also a Unicode escape that lets you print any Unicode character inside a string: \u{}. A hexadecimal number goes inside the {} to print it. Here is a short example of how to get the Unicode number, and how to print it again.

fn main() {
    println!("{:X}", '행' as u32); // Cast char as u32 to get the hexadecimal value
    println!("{:X}", 'H' as u32);
    println!("{:X}", '居' as u32);
    println!("{:X}", 'い' as u32);
    println!("\u{D589}, \u{48}, \u{5C45}, \u{3044}"); // Try printing them with unicode escape \u
}
---
D589
48
5C45
3044
행, H, 居, い

指定打印顺序

fn main() {
    let father_name = "Vlad";
    let son_name = "Adrian Fahrenheit";
    let family_name = "Țepeș";
    println!("This is {1} {2}, son of {0} {2}.", father_name, son_name, family_name);
}

---
This is Adrian Fahrenheit Țepeș, son of Vlad Țepeș.

指定别名打印

fn main() {
    println!(
        "{city1} is in {country} and {city2} is also in {country},
but {city3} is not in {country}.",
        city1 = "Seoul",
        city2 = "Busan",
        city3 = "Tokyo",
        country = "Korea"
    );
}

填充 pendding

{variable:padding alignment minimum.maximum}  {:填充字符 对齐方式 最小.最大}


fn main() {
    let letter1 = "123456789abcdefg";
    let letter2 = "12";
    println!("{:->5.10}", letter1);
    println!("{:->5.10}", letter2);
    println!("{:-^5.10}", letter2);
    println!("{:-<5.10}", letter2);
}
---

123456789a
---12
-12--
12---

参考

https://github.com/Dhghomon/easy_rust#more-about-printing

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值