tao-of-rust-codes icon indicating copy to clipboard operation
tao-of-rust-codes copied to clipboard

6.3.5 消费其 any 不再是一次遍历到底 利用try_fold 支持break

Open kingeasternsun opened this issue 4 years ago • 3 comments

页码与行数

  • 微信读书682页4行

文本或排版错误

暂无


代码错误

代码清单6-81 any的源码如下

    #[inline]
    #[stable(feature = "rust1", since = "1.0.0")]
    fn any<F>(&mut self, f: F) -> bool
    where
        Self: Sized,
        F: FnMut(Self::Item) -> bool,
    {
        #[inline]
        fn check<T>(mut f: impl FnMut(T) -> bool) -> impl FnMut((), T) -> ControlFlow<()> {
            move |(), x| {
                if f(x) { ControlFlow::BREAK } else { ControlFlow::CONTINUE }
            }
        }

        self.try_fold((), check(f)) == ControlFlow::BREAK
    }

  ...


    #[inline]
    #[stable(feature = "iterator_try_fold", since = "1.27.0")]
    fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R
    where
        Self: Sized,
        F: FnMut(B, Self::Item) -> R,
        R: Try<Output = B>,
    {
        let mut accum = init;
        while let Some(x) = self.next() {
            accum = f(accum, x)?;
        }
        try { accum }
    }

期待在第二版中可以讲下 try

Rust版本

$ rustc -V
rustc 1.57.0-nightly (8f8092cc3 2021-09-28)

错误信息

kingeasternsun avatar Oct 18 '21 10:10 kingeasternsun

@kingeasternsun 收到。感谢反馈

ZhangHanDong avatar Oct 18 '21 10:10 ZhangHanDong

@kingeasternsun 收到。感谢反馈

期望老师也可以在书里面讲下这个 ControlFlow

kingeasternsun avatar Nov 10 '21 09:11 kingeasternsun

@kingeasternsun 收到。感谢反馈

期望老师也可以在书里面讲下这个 ControlFlow

必须的,第二版里安排上了。

ZhangHanDong avatar Nov 10 '21 09:11 ZhangHanDong