为什么Rust有String
和str
?String
和之间有什么区别str
?什么时候使用String
而不是str
反之亦然?其中一个被弃用了吗?
在文字字符串和文字值等上下文中使用时,"文字"这个词的含义是什么?
字面值和值之间有什么区别?
这是我在Rust文档中看到的两个函数签名:
fn modify_foo(mut foo: Box<i32>) { *foo += 1; *foo }
fn modify_foo(foo: &mut i32) { *foo += 1; *foo }
Run Code Online (Sandbox Code Playgroud)
为什么不同的位置mut
?
似乎第一个函数也可以声明为
fn modify_foo(foo: mut Box<i32>) { /* ... */ }
Run Code Online (Sandbox Code Playgroud) let hello1 = "Hello, world!";
let hello2 = "Hello, world!".to_string();
let hello3 = String::from("Hello, world!");
Run Code Online (Sandbox Code Playgroud) 我正在使用一个hex!
只接受字符串文字的宏。我有一个从函数返回并存储在变量中的值。我无法对值进行硬编码并调用此函数。那么,如何hex!
使用变量调用宏?
这是我的工作代码:
let account: AccountId32 = hex_literal::hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"].into();
Run Code Online (Sandbox Code Playgroud)
这是我面临错误的代码:
let account_id = "d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d";
let account: AccountId32 = hex_literal::hex!(&account_id).into();
Run Code Online (Sandbox Code Playgroud)
错误是:
let account: AccountId32 = hex_literal::hex!["d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"].into();
Run Code Online (Sandbox Code Playgroud)
hex!
宏的所有示例仅使用字符串文字来演示它。