Crates.io | step |
lib.rs | step |
version | 0.2.0 |
source | src |
created_at | 2017-02-12 11:16:27.294754 |
updated_at | 2017-02-16 15:11:14.857376 |
description | A trait that allows for stepping numeric types. |
homepage | |
repository | https://github.com/ryanq/step |
max_upload_size | |
id | 8485 |
size | 4,688 |
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:
[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 } }
}