gpt4 book ai didi

string - 是否有 String::chars 的自有版本?

转载 作者:行者123 更新时间:2023-11-29 07:49:29 24 4
gpt4 key购买 nike

以下代码无法编译:

use std::str::Chars;

struct Chunks {
remaining: Chars,
}

impl Chunks {
fn new(s: String) -> Self {
Chunks {
remaining: s.chars(),
}
}
}

错误是:

error[E0106]: missing lifetime specifier
--> src/main.rs:4:16
|
4 | remaining: Chars,
| ^^^^^ expected lifetime parameter

Chars不拥有它迭代的字符并且它不能超过 &strString它是从创建的。

是否有Chars 的自有版本?不需要生命周期参数还是我必须保留 Vec<char>和我自己的索引?

最佳答案

还有 owned-chars crate , 哪个

provides an extension trait for String with two methods, into_chars and into_char_indices. These methods parallel String::chars and String::char_indices, but the iterators they create consume the String instead of borrowing it.

关于string - 是否有 String::chars 的自有版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com