简体   繁体   中英

How to define an infinite range of a generic integer type in Rust?

I have a generic type T that implements the num::Integer trait. I can write a T -based range iterator where e represents another value of type T denoting the upper bound of the range:

num::iter::range(T::one(), e)

The annoying thing is that it is necessarily bounded. How can I have an unbounded range? For primitive types, I can do something like (1..) . Can I do something like num::iter::range(T::one(), T::infinity()) ?

I can't find a function that does this in num::iter . I would like to be able to call functional style functions afterwards like map , filter etc...

这听起来像是loop而不是范围的用例。

Edit: Unfortunately, std::ops::RangeFrom is a struct, not a trait like for example the traits for infix operators in std::ops . So this is wrong, but since it has been accepted I can't delete it.

Original post : You can implement std::ops::RangeFrom for your type. This is the trait underlying the range syntax with only a lower bound, like eg 1.. .

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