简体   繁体   中英

How to count regex matches in Rust?

I'd like to count the matches of a regex in a string with Rust. I managed to print all matches:

let re = Regex::new(r"(?i)foo").unwrap();
let result = re.find_iter("This is foo and FOO foo as well as FoO.");
for i in result {
    println!("{}", i.as_str())
}

But I fail to simply get the number of matches. I can't find any function that gives me the count. I also tried size_hint() , but that does not work as well. Any way I can do that?

Here is the Scala version of what I'm looking for.

你已经有了迭代器,所以只计算迭代器中的元素数量:

re.find_iter("This is foo and FOO foo as well as FoO.").count()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM