#collections #buffers


Utilities for splitting sequences with range parameters

7 unstable releases (3 breaking)

0.4.0 Jan 28, 2021
0.3.0 Oct 28, 2020
0.2.0 Dec 1, 2019
0.2.0-beta.2 Nov 28, 2019
0.1.0-beta.1 Jun 16, 2019

#784 in Rust patterns

31 downloads per month
Used in strchunk

MIT license

256 lines

Splitting Sequences with Range Syntax

This library crate provides a trait and support utilities adding convenience methods for splitting sequences accordingly to a given range.

The primary use case for the TakeRange trait are data container libraries such as bytes, where representations of data buffers can be efficiently split into smaller parts, but the inherent methods for doing this tend to be not very mnemonic. Implementations of the trait parameterized with different range types provide convenient polymorphism with the range syntax.


This project is licensed under the MIT license.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in range-split by you, shall be licensed as MIT, without any additional terms or conditions.


Utilities for splitting sequences with range parameters.

The TakeRange trait provides polymorphic, easily memorizable methods for splitting indexed sequences with a parameter given in range syntax.

# #[cfg(features = "bytes")]
# fn main() {
# use bytes::Bytes;
use range_split::TakeRange;

let mut buf = Bytes::from("Hello, world");
let p = buf.take_range(..5);
assert_eq!(p, "Hello");
assert_eq!(buf, ", ");
# }
# #[cfg(not(features = "bytes"))]
# fn main() {}

is equivalent to

# #[cfg(features = "bytes")]
# fn main() {
# use bytes::Bytes;
let mut buf = Bytes::from("Hello, world");
let p = buf.split_to(5);
# }
# #[cfg(not(features = "bytes"))]
# fn main() {}

Implementations of TakeRange are provided for Bytes and BytesMut from the crate bytes if the bytes compile-time feature is enabled.