Uses old Rust 2015
0.2.0 |
|
---|---|
0.1.0 |
|
#182 in #unit
Used in ranged_set
4KB
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.
Using step
Add the crate to the dependencies section of Cargo.toml:
[dependencies]
step = { git = "https://github.com/ryanq/step" }
Then import the crate and type in your source:
extern crate step;
use step::Step;
Then you can use the functions for incrementing and decrementing numbers or implement it on your own types:
let number = 42;
assert_eq!(number.next(), 43);
assert_eq!(number.next_by(&3), 45);
assert_eq!(number.prev(), 41);
assert_eq!(number.prev_by(&3), 39);
struct Foo {
bar: i32,
}
impl Step for Foo {
fn next(&self) -> Self { Foo { bar: self.bar + 1 } }
fn next_by(&self, by: &Self) -> Self { Foo { bar: self.bar + *by } }
fn prev(&self) -> Self { Foo { bar: self.bar - 1 } }
fn prev_by(&self, by: &Self) -> Self { Foo { bar: self.bar - *by } }
}