【发布时间】:2015-04-30 20:20:21
【问题描述】:
我想使用Peekable 作为新cautious_take_while 操作的基础,该操作类似于IteratorExt 中的take_while,但不消耗第一个失败的项目。 (还有一个问题是这是否是一个好主意,以及是否有更好的方法来在 Rust 中实现这个目标——我很乐意得到这个方向的提示,但主要是我试图理解我的代码在哪里打破)。
我尝试启用的 API 基本上是:
let mut chars = "abcdefg.".chars().peekable();
let abc : String = chars.by_ref().cautious_take_while(|&x| x != 'd');
let defg : String = chars.by_ref().cautious_take_while(|&x| x != '.');
// yielding (abc = "abc", defg = "defg")
我在creating a MCVE here 上试了一下,但我得到了:
:10:5: 10:19 错误:无法移出借来的内容 :10 chars.by_ref().cautious_take_while(|&x| x != '.');
据我所知,就我的函数签名而言,我遵循与 Rust 自己的TakeWhile 相同的模式,但我从借用检查器中看到了不同的不同行为。有人能指出我做错了什么吗?
【问题讨论】:
标签: iterator rust traits borrow-checker