This crate provides extension traits for
Vec<u8> that enable
their use as byte strings, where byte strings are conventionally UTF-8. This
differs from the standard library's
str types in that they are
not required to be valid UTF-8, but may be fully or partially valid UTF-8.
When should I use byte strings?
See this part of the documentation for more details: https://docs.rs/bstr/1.*/bstr/#when-should-i-use-byte-strings.
The short story is that byte strings are useful when it is inconvenient or incorrect to require valid UTF-8.
cargo add bstr
The following two examples exhibit both the API features of byte strings and the I/O convenience functions provided for reading line-by-line quickly.
This first example simply shows how to efficiently iterate over lines in stdin, and print out lines containing a particular substring:
use ; use ;
This example shows how to count all of the words (Unicode-aware) in stdin, line-by-line:
use ; use ;
This example shows how to convert a stream on stdin to uppercase without performing UTF-8 validation and amortizing allocation. On standard ASCII text, this is quite a bit faster than what you can (easily) do with standard library APIs. (N.B. Any invalid UTF-8 bytes are passed through unchanged.)
use ; use ;
This example shows how to extract the first 10 visual characters (as grapheme clusters) from each line, where invalid UTF-8 sequences are generally treated as a single character and are passed through correctly:
use ; use ;
This crates comes with a few features that control standard library, serde and Unicode support.
std- Enabled by default. This provides APIs that require the standard library, such as
PathBuf. Enabling this feature also enables the
alloc- Enabled by default. This provides APIs that require allocations via the
alloccrate, such as
unicode- Enabled by default. This provides APIs that require sizable Unicode data compiled into the binary. This includes, but is not limited to, grapheme/word/sentence segmenters. When this is disabled, basic support such as UTF-8 decoding is still included. Note that currently, enabling this feature also requires enabling the
stdfeature. It is expected that this limitation will be lifted at some point.
serde- Enables implementations of serde traits for
BStr, and also
Minimum Rust version policy
This crate's minimum supported
rustc version (MSRV) is
In general, this crate will be conservative with respect to the minimum supported version of Rust. MSRV may be bumped in minor version releases.
Since it is plausible that some of the types in this crate might end up in your
public API (e.g.,
BString), we will commit to being very
conservative with respect to new major version releases. It's difficult to say
precisely how conservative, but unless there is a major issue with the
release, I wouldn't expect a
2.0 release to come out any sooner than some
period of years.
A large part of the API surface area was taken from the standard library, so from an API design perspective, a good portion of this crate should be on solid ground. The main differences from the standard library are in how the various substring search routines work. The standard library provides generic infrastructure for supporting different types of searches with a single method, where as this library prefers to define new methods for each type of search and drop the generic infrastructure.
Some probable future considerations for APIs include, but are not limited to:
- Unicode normalization.
- More sophisticated support for dealing with Unicode case, perhaps by
combining the use cases supported by
Here are some examples that are probably out of scope for this crate:
- Regular expressions.
- Unicode collation.
The exact scope isn't quite clear, but I expect we can iterate on it.
In general, as stated below, this crate brings lots of related APIs together
into a single crate while simultaneously attempting to keep the total number of
dependencies low. Indeed, every dependency of
bstr, except for
High level motivation
Strictly speaking, the
bstr crate provides very little that can't already be
achieved with the standard library
&[u8] APIs and the ecosystem of
library crates. For example:
- The standard library's
Utf8Errorcan be used for incremental lossy decoding of
unicode-segmentationcrate can be used for iterating over graphemes (or words), but is only implemented for
&strtypes. One could use
Utf8Errorabove to implement grapheme iteration with the same semantics as what
bstrprovides (automatic Unicode replacement codepoint substitution).
twowaycrate can be used for fast substring searching on
So why create
bstr? Part of the point of the
bstr crate is to provide a
uniform API of coupled components instead of relying on users to piece together
loosely coupled components from the crate ecosystem. For example, if you wanted
to perform a search and replace in a
Vec<u8>, then writing the code to do
that with the
twoway crate is not that difficult, but it's still additional
glue code you have to write. This work adds up depending on what you're doing.
Consider, for example, trimming and splitting, along with their different
In other words,
bstr is partially a way of pushing back against the
micro-crate ecosystem that appears to be evolving. Namely, it is a goal of
bstr to keep its dependency list lightweight. For example,
serde is an
optional dependency because there is no feasible alternative. In service of
this philosophy, currently, the only required dependency of
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
The data in
src/unicode/data/ is licensed under the Unicode License Agreement
this data is only used in tests.