相关疑难解决方法(0)

Rust的`String`和`str`之间有什么区别?

为什么Rust有StringstrString和之间有什么区别str?什么时候使用String而不是str反之亦然?其中一个被弃用了吗?

string rust

341
推荐指数
13
解决办法
4万
查看次数

"文字"这个词是什么意思?

在文字字符串和文字值等上下文中使用时,"文字"这个词的含义是什么?

字面值和值之间有什么区别?

c# language-agnostic glossary terminology

72
推荐指数
6
解决办法
4万
查看次数

在变量名之前和":"之后放置"mut"有什么区别?

这是我在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)

variables syntax reference mutable rust

51
推荐指数
3
解决办法
5734
查看次数

这三种在Rust中声明字符串的方法有什么区别?

let hello1 = "Hello, world!";
let hello2 = "Hello, world!".to_string();
let hello3 = String::from("Hello, world!");
Run Code Online (Sandbox Code Playgroud)

string rust

9
推荐指数
1
解决办法
340
查看次数

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

rust

0
推荐指数
1
解决办法
1051
查看次数