[](https://github.com/qubit-ltd/rs-utils/actions/workflows/ci.yml)
[](https://qubit-ltd.github.io/rs-utils/coverage/)
[](https://crates.io/crates/qubit-utils)
[](https://www.rust-lang.org)
[](LICENSE)
[](README.zh_CN.md)
General-purpose, dependency-light utilities for Rust projects. This crate gives
Qubit applications one shared implementation for standard-range algebra,
fallible allocation, runtime-only values, and audited low-level slice access.
Rust developers who need small, reusable building blocks across Qubit projects
without adopting a broad framework or duplicating boundary-sensitive code.
Add the published crate to your Cargo.toml:
toml
[dependencies]
qubit-utils = "0.3"
Suppose a service accepts a requested numeric window but may process only values
inside a configured policy window. The range helpers accept standard Rust range
syntax and return owned standard-library bounds:
rust
use std::ops::Bound::Excluded;
use std::ops::Bound::Included;
use qubit_utils::range::encloses;
use qubit_utils::range::intersection;
let policy = 1..10;
let requested = 5..=20;
let accepted = intersection(&policy, &requested);
assert_eq!(accepted, Some((Included(5), Excluded(10))));
assert!(encloses(&policy, &accepted.expect("the windows overlap")));
The result is a (Bound<T>, Bound<T>), which already implements
RangeBounds<T> and can be passed to ordered standard-library collection APIs.
Boundary-heavy helpers are easy to get almost right and costly to maintain in
multiple crates. qubit-utils centralizes the small contracts that recur across
Qubit software while preserving standard Rust representations and keeping the
default dependency surface small.
The 0.3.0 crate provides several reusable utility APIs:
- standard-range relationships and algebra (is_empty, encloses, overlaps,
is_connected, intersection, span, gap, and compare),
- fallible allocation helpers (create_vec, try_reserve_vec, try_reserve_string,
allocation_error, plus coverage-testing helpers under coverage cfg),
- nonzero helper for NonZeroUsize,
- Transient<T> for runtime-only state excluded from Eq/Hash,
- UncheckedSlice and SliceRange for low-level slice/array boundary checks.
The range module works with std::ops::RangeBounds, including normal Rust range
syntax and arbitrary (Bound<T>, Bound<T>) pairs. It adds stable free functions
for the range relationships and operations that are not yet available as a
complete stable standard-library API.
The remaining modules provide reusable memory, numeric, slice, and runtime-state
helpers used by adjacent Qubit Rust crates. Existing crate-root re-exports remain
available for their commonly used types and functions.
Range algebra other than is_empty requires a total order (Ord). Use an
explicit ordered wrapper when working with floating-point values whose NaN
semantics need a policy. Range emptiness follows endpoint-order semantics and
does not canonicalize a discrete domain, so an open integer range such as
(1, 2) is not treated as empty merely because no integer lies between its
endpoints.
The crate intentionally does not define a custom interval type, dynamic
comparators, discrete-domain stepping, or domain-specific date and money range
types. Additional helpers are added only when they are broadly reusable.
bash
cargo test
cargo test --all-features
./ci-check.sh
./coverage.sh
Copyright (c) 2025 - 2026. Haixing Hu. All rights reserved.
Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the
full license text.
Contributions are welcome. Please follow the Rust API guidelines, keep public
API documentation and tests current, and run ./align-ci.sh to format code and
./ci-check.sh to satisfy CI requirements before submitting a pull request.
Haixing Hu - *Qubit Co. Ltd.*
Repository: [https://github.com/qubit-ltd/rs-utils](https://github.com/qubit-ltd/rs-utils)