如何连接以下类型组合:
str 和 strString 和 strString 和 String我正在尝试连接静态字符串和字符串文字以构建另一个静态字符串.以下是我能想到的最好的,但它不起作用:
const DESCRIPTION: &'static str = "my program";
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const VERSION_STRING: &'static str = concat!(DESCRIPTION, " v", VERSION);
Run Code Online (Sandbox Code Playgroud)
在Rust中有没有办法做到这一点,或者我必须一遍又一遍地写相同的文字?
基板开发人员可能会遇到的一个常见问题是:开发自定义托盘以将映射存储到具有常见类型的存储中,例如String. 举个例子:
#[derive(Encode, Decode, Clone, Default, RuntimeDebug)]
pub struct ClusterMetadata {
ip_address: String,
namespace: String,
whitelisted_ips: String,
}
Run Code Online (Sandbox Code Playgroud)
在构建运行时,您会收到每个错误String:
|
21 | ip_address: String,
| ^^^^^^ not found in this scope
Run Code Online (Sandbox Code Playgroud)
Strings不包括在范围内?和其他std锈类型?我有什么理由不能将字符串文字与字符串变量连接起来吗?以下代码:
fn main() {
let x = ~"abcd";
io::println("Message: " + x);
}
Run Code Online (Sandbox Code Playgroud)
给出了这个错误:
test2.rs:3:16: 3:31 error: binary operation + cannot be applied to type `&'static str`
test2.rs:3 io::println("Message: " + x);
^~~~~~~~~~~~~~~
error: aborting due to previous error
Run Code Online (Sandbox Code Playgroud)
我想这是一个非常基本且非常常见的模式,fmt!在这种情况下的使用只会带来不必要的混乱.