step
step
is a crate that provides the trait Step
, which allows for unit
steps and arbitrary steps on numeric types.
Documentation can be found on [docs.rs].
step
Add the crate to the dependencies section of Cargo.toml:
toml
[dependencies]
step = { git = "https://github.com/ryanq/step" }
Then import the crate and type in your source:
```rust extern crate step;
use step::Step; ```
Then you can use the functions for incrementing and decrementing numbers or implement it on your own types:
```rust let number = 42;
asserteq!(number.next(), 43); asserteq!(number.nextby(&3), 45); asserteq!(number.prev(), 41); asserteq!(number.prevby(&3), 39); ```
```rust struct Foo { bar: i32, }
impl Step for Foo { fn next(&self) -> Self { Foo { bar: self.bar + 1 } } fn nextby(&self, by: &Self) -> Self { Foo { bar: self.bar + *by } } fn prev(&self) -> Self { Foo { bar: self.bar - 1 } } fn prevby(&self, by: &Self) -> Self { Foo { bar: self.bar - *by } } } ```