如何在 Rust 中将变量作为字符串文字传递?

Avi*_*ava 0 rust

我正在使用一个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!宏的所有示例仅使用字符串文字来演示它。

She*_*ter 5

你不能。从字面上看这是不可能的。文字是在程序的源代码中逐字写入值的东西。

不要使用该宏,因为它没有做你想要的(强调我的):

这个 crate 提供hex!在编译时将十六进制字符串文字转换为字节数组的宏。

也许你想要六角板条箱

也可以看看:


归档时间:

查看次数:

1051 次

最近记录:

5 年,10 月 前