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.step(), 43); asserteq!(number.stepby(&3), 45); asserteq!(number.stepback(), 41); asserteq!(number.stepbackby(&3), 39); ```
```rust struct Foo { bar: i32, }
impl Step for Foo { fn step(&self) -> Self { Foo { bar: self.bar + 1 } } fn stepby(&self, by: &Self) -> Self { Foo { bar: self.bar + *by } } fn stepback(&self) -> Self { Foo { bar: self.bar - 1 } } fn stepbackby(&self, by: &Self) -> Self { Foo { bar: self.bar - *by } } } ```