0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ある条件を満たす要素の数を求めたい

0
Posted at

ある条件を満たす要素の数を求めたい場合、filterとcountを組み合わせます。

例: 3の倍数の数を数える

fn main() {
    let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    let count = numbers.iter()
        .filter(|&&x| x % 3 == 0)
        .count();

    println!("{}", count); //=> 3
}

環境情報

$ rustc -V
rustc 1.76.0 (07dca489a 2024-02-04)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?