小编Don*_*owe的帖子

将Vec <String>转换为Rust中的&str片段?

史蒂夫Klabnik的书面记录上的差异之间的预锈1.0文档中String&str,拉斯特你应该使用&str,除非你真的需要有一个以上的所有权String.同样,建议使用对slice(&[])的引用而不是Vecs,除非你真的需要对the的所有权Vec.

我有一个Vec<String>和我想写一个使用字符串的这个序列的功能,它并不需要在所有权VecString实例,应该说功能拍摄&[&str]?如果是这样,参考Vec<String>进入的最佳方式是&[&str]什么?或者,这种胁迫是否过度杀伤?

string reference vector ownership rust

22
推荐指数
2
解决办法
4140
查看次数

让 Enumerate 在 Rust 中作为 ExactSizeIterator 工作

我想使用 RustEnumerate从每次迭代中获取切片中的字符及其索引:

fn main() {
    for (j, val) in "dummy string".chars().enumerate().rev() {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译时cargo run我得到:

error: the trait `core::iter::ExactSizeIterator` is not implemented for the type `core::str::Chars<'_>` [E0277]
    for (j, val) in "dummy string".chars().enumerate().rev() {
                                                       ^~~
help: see the detailed explanation for E0277

error: the trait `core::iter::ExactSizeIterator` is not implemented for the type `core::str::Chars<'_>` [E0277]
    for (j, val) in "dummy string".chars().enumerate().rev() {
        // ...
    }
Run Code Online (Sandbox Code Playgroud)

我可以理解为什么这会失败:该rev方法需要 anExactSizeIterator因为它需要从一开始就知道切片中的最后一个元素及其索引。在这种情况下是否可以得到 an ExactSizeIterator,或者迭代器的长度是否需要在编译时加入?如果可能的话,是否只是用类似的东西指定迭代器的问题 …

rust

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

标签 统计

rust ×2

ownership ×1

reference ×1

string ×1

vector ×1