1个稳定版本
1.0.0 | 2024年3月1日 |
---|
#1267 in Rust模式
5KB
更大的S:更好的字符串字面量
每个人都厌倦了输入 "foo"._string()
和 String::from("bar")
这类东西。也许你已经尝试过 Big S,这是一个只有一个函数的单行有用crate。使用"Big S",你可以这样做
assert_eq!(S("foo"), "foo".to_string())
不错,但事情可以更好,使用 更大的S,同样只有一个宏 S
。
用法
空字符串
- Big S:
S)
- 更大的S:
S!)
字符串
- Big S:
S)
- 更大的S:
S!)
连接字符串
这一项是新的。
- 常见:
foo.to_string() + "bar"
- Big S:
S + "bar"
- 更大的S:
S! "foo" "bar"
如果你想要用空格分隔字符串,你可以这样做
assert_eq!(S!("hello", "world"), "hello world".to_string());
这对于处理大量的字符串字面量特别有帮助
assert_eq!(
S!(
"Imagine how stupid it will be to include a very long paragraph literal in rust code.",
"With even bigger s, text has much more readability.",
"Have you ever seen a very very long String literal in your code?"
"Well, I have. I guess I won't see that any more.",
),
"Imagine how stupid it will be to include a very long paragraph literal in rust code. With even bigger s, text has much more readability. Have you ever seen a very very long String literal in your code? Well, I have. I guess I won't see that any more.".to_string()
)