Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust print! 这么多输出一个 % 符号 #4

Open
xgqfrms opened this issue Sep 15, 2022 · 1 comment
Open

Rust print! 这么多输出一个 % 符号 #4

xgqfrms opened this issue Sep 15, 2022 · 1 comment
Labels
% bug % bug print! & println! print! & println!

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Sep 15, 2022

Rust print! 这么多输出一个 % 符号

image

image

image

使用 \n 修复,末尾的 % 问题 ✅

fn main () {
  // print
  print!("print 🦀");
  let year = 2022;
  print!("this year is {}", year);
  // 使用 \n 修复,末尾的 % 问题 ✅
  print!("\n");
}

/*

$ rustc ./print.rs && ./print
// print 🦀%

// ??? Rust print! 这么多输出一个 %
*/
@xgqfrms xgqfrms added the print! & println! print! & println! label Sep 15, 2022
@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 15, 2022

https://www.runoob.com/rust/rust-println.html

println 后面还有一个 ! 符号,难道 Rust 函数之后都要加一个感叹号?

显然并不是这样, println 不是一个函数而是一个宏规则;

Rust 输出文字的方式主要有两种:println!()print!()

这两个"函数"都是向命令行输出字符串的方法,区别仅在于前者会在输出的最后附加输出一个换行符。

当用这两个"函数"输出信息的时候,第一个参数是格式字符串,后面是一串可变参数,对应着格式字符串中的"占位符",这一点与 C 语言中的 printf 函数很相似。

但是,Rust 中格式字符串中的占位符不是 "% + 字母" 的形式,而是一对 {}

@xgqfrms xgqfrms added the % bug % bug label Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
% bug % bug print! & println! print! & println!
Projects
None yet
Development

No branches or pull requests

1 participant