7 个版本
0.4.2 | 2022 年 1 月 5 日 |
---|---|
0.4.1 | 2022 年 1 月 1 日 |
0.4.0 | 2021 年 12 月 31 日 |
0.3.0 | 2021 年 12 月 30 日 |
0.1.0 |
|
#1100 在 算法
16KB
189 行
提供对满足 RangeBounds<T: Copy + Default + Step>
的任何类型的递增和递减操作的真正零成本的替代方案。
此 crate 实现的两个 trait 方法(inc_by
和 dec_by
)生成的汇编代码,在绝大多数情况下应该与递增 "step" 基准的 while
循环或递减 "step" 基准的 while
循环生成的汇编代码几乎或完全相同。如果你遇到不是这种情况的场景,请随时就此事提交一个问题。
最低支持的 Rust 版本:由于使用了 Step
trait,该 trait 尚未稳定,因此此 crate 目前仅适用于 nightly。
#![no_std]
兼容性:此 crate 默认完全兼容 #![no_std]
。
基本使用示例
use staticstep::*;
// Apart from aiming to provide a properly-optimized Rust equivalent to the sort of C-style for-loop
// that ends in `i += number` or `i -= number` as opposed to `i++` or `i--`, this crate also aims to
// (and does) support backwards ranges in a meaningful way that's logically equivalent to how
// forwards ranges are generally dealt with in Rust.
fn main() {
// Exclusive, so 48 is the last number printed.
for i in (0..64).inc_by::<16>() {
print!("{} ", i);
}
println!("");
// Inclusive, so 64 is the last number printed.
for i in (0..=64).inc_by::<16>() {
print!("{} ", i);
}
println!("");
// Exclusive, so 16 is the last number printed.
for i in (64..0).dec_by::<16>() {
print!("{} ", i);
}
println!("");
// Inclusive, so 0 is the last number printed.
for i in (64..=0).dec_by::<16>() {
print!("{} ", i);
}
// Note that `inc_by` will always immediately return `None` if given a reverse range, while
// `dec_by` will always immediately return `None` if given a "normal" forwards range.
}
许可
根据 MIT 许可证 或 Apache 许可证 版本 2.0 许可。任选其一!任何源代码贡献将以相同的方式双重许可。