本文簡要介紹rust語言中 std::iter::Iterator.count
的用法。
用法
fn count(self) -> usize
使用迭代器,計算迭代次數並返回。
此方法將重複調用 next
直到遇到 None
,返回它看到 Some
的次數。請注意,即使迭代器沒有任何元素,也必須至少調用一次 next
。
溢出行為
該方法沒有防止溢出,因此計算具有超過 usize::MAX
個元素的迭代器的元素會產生錯誤的結果或Panics。如果啟用了調試斷言,則保證會出現Panics。
Panics
如果迭代器有超過 usize::MAX
個元素,此函數可能會出現Panics。
例子
基本用法:
let a = [1, 2, 3];
assert_eq!(a.iter().count(), 3);
let a = [1, 2, 3, 4, 5];
assert_eq!(a.iter().count(), 5);
相關用法
- Rust Iterator.copied用法及代碼示例
- Rust Iterator.collect用法及代碼示例
- Rust Iterator.cmp用法及代碼示例
- Rust Iterator.cmp_by用法及代碼示例
- Rust Iterator.chain用法及代碼示例
- Rust Iterator.cloned用法及代碼示例
- Rust Iterator.cycle用法及代碼示例
- Rust Iterator.skip_while用法及代碼示例
- Rust Iterator.max_by_key用法及代碼示例
- Rust Iterator.is_sorted用法及代碼示例
- Rust Iterator.is_partitioned用法及代碼示例
- Rust Iterator.intersperse用法及代碼示例
- Rust Iterator.scan用法及代碼示例
- Rust Iterator.min用法及代碼示例
- Rust Iterator.find_map用法及代碼示例
- Rust Iterator.peekable用法及代碼示例
- Rust Iterator.rev用法及代碼示例
- Rust Iterator.product用法及代碼示例
- Rust Iterator.any用法及代碼示例
- Rust Iterator.max用法及代碼示例
- Rust Iterator.by_ref用法及代碼示例
- Rust Iterator.min_by用法及代碼示例
- Rust Iterator.inspect用法及代碼示例
- Rust Iterator.min_by_key用法及代碼示例
- Rust Iterator.intersperse_with用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::iter::Iterator.count。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。