3 个不稳定版本
0.2.1 | 2020年8月3日 |
---|---|
0.2.0 | 2020年7月25日 |
0.1.0 | 2020年3月8日 |
#1676 in 文本处理
10KB
147 行
interpol
Rust 的字符串插值宏
用法
此 crate 提供的宏与 std 的格式化宏(format
、print
、println
、eprint
、eprintln
等)相对应。
您可以使用这些宏与 std 中的宏一样。
use interpol::println;
let a = 3;
let b = 4;
println!("{a} times {b} is {a * b}."); // => "3 times 4 is 12."
它还可以指定格式化选项
let pi = 3.14;
println!("{pi:.10}"); // => "3.1400000000"
println!("{123:x}"); // => "7b"
let v = vec![1, 2, 3];
println!("{v:?}"); // => "[1, 2, 3]"
正确处理 Unicode 字符
let world = "世界";
println!("ハロー {world}"); // => "ハロー 世界"
能够编写表达式
println!("PI = {std::f64::consts::PI:.10}");
// => "PI = 3.1415926536"
println!("PI = { 1.0_f64.atan() * 4.0 :.10}");
// => "PI = 3.1415926536"
您还可以编写嵌套块表达式
println!("{ { let t = 123; t * t } }"); // => "15129"
println!("{{ let t = 123; t * t }}");
// "{{" and "}}" is escaped so the result is:
// => "{ let t = 123; t * t }"
依赖项
~1.5MB
~35K SLoC