Skip to content

[Practice] 为什么出现了预料之外的结果? #4

Open
@ZhangHanDong

Description

@ZhangHanDong
fn main() {
    let a = [1, 2, 3];
    let mut iter = a.iter();    
    assert_eq!(iter.any(|&x| x != 2), true);
    let sum = iter.fold(0, |acc, &x| acc + x);
    assert_eq!(sum, 6);
}

此代码运行报错:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `5`,
 right: `6`', src/main.rs:6:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.

请解释,为什么会出现这种错误?

——————

考察点:迭代器

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions