Skip to content

Commit 809a1a8

Browse files
committed
mark str::string::String.trim.* functions as #[must_use].
The functions return a reference to a new object and do not modify in-place as the following code shows: ```` let s = String::from(" hello "); s.trim(); assert_eq!(s, " hello "); ```` The new reference should be bound to a variable as now indicated by #[must_use].
1 parent 79d8a0f commit 809a1a8

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libcore/str/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,6 +3544,8 @@ impl str {
35443544
///
35453545
/// assert_eq!("Hello\tworld", s.trim());
35463546
/// ```
3547+
#[must_use = "this returns the trimmed string as a new allocation, \
3548+
without modifying the original"]
35473549
#[stable(feature = "rust1", since = "1.0.0")]
35483550
pub fn trim(&self) -> &str {
35493551
self.trim_matches(|c: char| c.is_whitespace())
@@ -3579,6 +3581,8 @@ impl str {
35793581
/// let s = " עברית ";
35803582
/// assert!(Some('ע') == s.trim_start().chars().next());
35813583
/// ```
3584+
#[must_use = "this returns the trimmed string as a new allocation, \
3585+
without modifying the original"]
35823586
#[stable(feature = "trim_direction", since = "1.30.0")]
35833587
pub fn trim_start(&self) -> &str {
35843588
self.trim_start_matches(|c: char| c.is_whitespace())
@@ -3614,6 +3618,8 @@ impl str {
36143618
/// let s = " עברית ";
36153619
/// assert!(Some('ת') == s.trim_end().chars().rev().next());
36163620
/// ```
3621+
#[must_use = "this returns the trimmed string as a new allocation, \
3622+
without modifying the original"]
36173623
#[stable(feature = "trim_direction", since = "1.30.0")]
36183624
pub fn trim_end(&self) -> &str {
36193625
self.trim_end_matches(|c: char| c.is_whitespace())
@@ -3716,6 +3722,8 @@ impl str {
37163722
/// ```
37173723
/// assert_eq!("1foo1barXX".trim_matches(|c| c == '1' || c == 'X'), "foo1bar");
37183724
/// ```
3725+
#[must_use = "this returns the trimmed string as a new allocation, \
3726+
without modifying the original"]
37193727
#[stable(feature = "rust1", since = "1.0.0")]
37203728
pub fn trim_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
37213729
where P::Searcher: DoubleEndedSearcher<'a>
@@ -3761,6 +3769,8 @@ impl str {
37613769
/// let x: &[_] = &['1', '2'];
37623770
/// assert_eq!("12foo1bar12".trim_start_matches(x), "foo1bar12");
37633771
/// ```
3772+
#[must_use = "this returns the trimmed string as a new allocation, \
3773+
without modifying the original"]
37643774
#[stable(feature = "trim_direction", since = "1.30.0")]
37653775
pub fn trim_start_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str {
37663776
let mut i = self.len();
@@ -3804,6 +3814,8 @@ impl str {
38043814
/// ```
38053815
/// assert_eq!("1fooX".trim_end_matches(|c| c == '1' || c == 'X'), "1foo");
38063816
/// ```
3817+
#[must_use = "this returns the trimmed string as a new allocation, \
3818+
without modifying the original"]
38073819
#[stable(feature = "trim_direction", since = "1.30.0")]
38083820
pub fn trim_end_matches<'a, P: Pattern<'a>>(&'a self, pat: P) -> &'a str
38093821
where P::Searcher: ReverseSearcher<'a>

0 commit comments

Comments
 (0)