Today, str::split_ascii_whitespace exists as an alternative to str::split_whitespace so users do not have to sacrifice performance when they are not concerned with Unicode. Even on ASCII-only text, split_ascii_whitespace is considerably faster than split_whitespace.
The added ASCII trim functions would be an extension of that performance-oriented concept and make the API more consistent between [u8] and &str, mirroring the currently unstable byte slice functions trim_ascii_start, trim_ascii_end, and trim_ascii tracked in https://github.com/rust-lang/rust/issues/94035.
Alternatively, users could use str::as_bytes to trim using u8::is_ascii_whitespace, but they would have to use the unsafe str::from_utf8_unchecked or re-validate with the safe str::from_utf8 to return a &str.
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
We think this problem seems worth solving, and the standard library might be the right place to solve it.
We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Proposal
Problem statement
There is no safe and efficient way to remove leading or trailing ASCII whitespace on
&str
incore
today.Motivating examples or use cases
The currently available
str::trim
function family[^1] is Unicode-aware, which makes users pay a performance cost due to Unicode code point processing.Additionally, using
str::trim
may increase code size due tochar::is_whitespace
using a 256 byte lookup table to determine Unicode whitespace.Today,
str::split_ascii_whitespace
exists as an alternative tostr::split_whitespace
so users do not have to sacrifice performance when they are not concerned with Unicode. Even on ASCII-only text,split_ascii_whitespace
is considerably faster thansplit_whitespace
.The added ASCII trim functions would be an extension of that performance-oriented concept and make the API more consistent between
[u8]
and&str
, mirroring the currently unstable byte slice functionstrim_ascii_start
,trim_ascii_end
, andtrim_ascii
tracked in https://github.com/rust-lang/rust/issues/94035.Solution sketch
See https://github.com/rust-lang/rust/pull/118523
This proposal seeks to implement the following functions on
&str
, leveraging the pre-existing functions on byte slices.Adding these functions would remove the need for users to write unsafe code to efficiently implement the behavior themselves.
Alternatives
Do nothing. This can be implemented safely by users with
trim_matches
,trim_start_matches
, andtrim_end_matches
.Alternatively, users could use
str::as_bytes
to trim usingu8::is_ascii_whitespace
, but they would have to use the unsafestr::from_utf8_unchecked
or re-validate with the safestr::from_utf8
to return a&str
.Links and related work
Add ASCII whitespace trimming functions to &str - https://github.com/rust-lang/rust/pull/118523
Tracking Issue for ASCII trim functions on byte slices - https://github.com/rust-lang/rust/issues/94035
https://github.com/rust-lang/rust/issues/94035 contains some discussion and desire for adding these functions to
&str
.What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
[^1]:
str::trim
,str::trim_end
,str::trim_start
,str::trim_matches