Rust stdlib is not about elegance, but about correctness and efficiency.

In your particular case, trim() is defined as str::trim(&self) -> &str because it always returns a substring of the original string, so it does not need to copy or allocate a new string, just compute the begin and end, and do the slice.

But to_lowercase() is defined as str::to_lowercase(&self) -> String because it changes each of its characters to the lowercase equivalent, so it must allocate and fill a new String.

You may thing that if you own the string you can mutate it to lowercase in-place. But that will not work in general because there is not a 1-to-1 map between lowercase and uppercase letters. Think of, for example รŸ <-> SS in German.

Naturally, you may know that your string only has ASCII characters... if so you can also use str::make_ascii_lowercase(&mut self) that does the change in-place, but only for ASCII characters that do have the 1-to-1 map.

So, summing up, the more ergonomic code would be, to trim input and copy to an owned lowercase:

let guess : String = input.trim().to_lowercase();

Or if you absolutely want to avoid allocating an extra string, but you are positive that only ASCII characters matter:

let mut input = input; //you could also add the mut above
input.make_ascii_lowercase();
let guess: &str = input.trim();
Answer from rodrigo on Stack Overflow
๐ŸŒ
Rust
doc.rust-lang.org โ€บ std
"to_lowercase" Search
Rust strs are typically accessed as immutable references: &str. Use the owned String for building and mutating strings. For converting to strings use the format!
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-an-uppercase-letter-or-string-to-lowercase-in-rust
How to convert an uppercase letter or string to lowercase in Rust
It converts an uppercase character to its lowercase equivalent. ... For strings, Rust provides the to_lowercase() method on the String and str types.
Discussions

trying to trim and lowercase my String in rust - Stack Overflow
I'm trying to trim and lowercase my String. Currently I have use dialoguer::Input; let input: String = Input::new() .with_prompt("Guess a 5 letter word") .interact_text() . More on stackoverflow.com
๐ŸŒ stackoverflow.com
Performance issue: can I avoid of using the slow to_lowercase function?
You want unicase . More on reddit.com
๐ŸŒ r/rust
11
14
August 27, 2017
Add `as_lowercase` et. al - libs - Rust Internals
There exists a method called str::to_lowercase that allocates a string with unicode case conversion. I propose adding a method called str::as_lowercase with the signature impl str { fn as_lowercase(&self) -> impl Display + '_; } Using this method can avoid some allocations, which is perhaps ... More on internals.rust-lang.org
๐ŸŒ internals.rust-lang.org
7
December 15, 2021
Char to_lowercase problem
Hi, I tried to convert a char to lowercase, but compile report a type error, any suggestion? Thanks! use std::collections::BTreeMap; fn main() { let mut new_btm: BTreeMap = BTreeMap::new(); let c = 'Cโ€ฆ More on users.rust-lang.org
๐ŸŒ users.rust-lang.org
2
0
April 11, 2021
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ to-lowercase-rust
Rust - String to_lowercase Example - Dot Net Perls
Info With make_ascii_lowercase, the original data is modified in-place, so we must have a String. Here We call to_string() on the str literal to create a String, so that it can be modified in-place.
Top answer
1 of 2
19

Rust stdlib is not about elegance, but about correctness and efficiency.

In your particular case, trim() is defined as str::trim(&self) -> &str because it always returns a substring of the original string, so it does not need to copy or allocate a new string, just compute the begin and end, and do the slice.

But to_lowercase() is defined as str::to_lowercase(&self) -> String because it changes each of its characters to the lowercase equivalent, so it must allocate and fill a new String.

You may thing that if you own the string you can mutate it to lowercase in-place. But that will not work in general because there is not a 1-to-1 map between lowercase and uppercase letters. Think of, for example รŸ <-> SS in German.

Naturally, you may know that your string only has ASCII characters... if so you can also use str::make_ascii_lowercase(&mut self) that does the change in-place, but only for ASCII characters that do have the 1-to-1 map.

So, summing up, the more ergonomic code would be, to trim input and copy to an owned lowercase:

let guess : String = input.trim().to_lowercase();

Or if you absolutely want to avoid allocating an extra string, but you are positive that only ASCII characters matter:

let mut input = input; //you could also add the mut above
input.make_ascii_lowercase();
let guess: &str = input.trim();
2 of 2
1

Try this:

let s = "   aBcD ";
let s2 = s.trim().to_lowercase();
println!("[{s}], [{s2}]");

The above will work if s is &str (as in my example) or String and it will print:

[   aBcD ], [abcd]

So the last line in your code (if you insist on having guess as &str) should become:

let guess: &str = &input.trim().to_lowercase();

Otherwise if you write just:

let guess = input.trim().to_lowercase();

, guess will be of type String, as that's what to_lowercase() returns.

๐ŸŒ
Smolkit
smolkit.com โ€บ blog โ€บ posts โ€บ how-to-convert-string-to-lower-case-in-rust
How to Convert String to Lower Case in Rust? | - smolkit.com
July 3, 2023 - Introduction Naming conventions ... to code readability and maintainability. In Rust, following consistent naming conventions can help you write clean and organized code. This article will provide an overview of recommended naming conventions for files, classes, interfaces, functions, variables, constants, modules, and libraries in Rust. Files Rust source files typically follow the convention of using lowercase letters, with ...
๐ŸŒ
Reddit
reddit.com โ€บ r/rust โ€บ performance issue: can i avoid of using the slow to_lowercase function?
r/rust on Reddit: Performance issue: can I avoid of using the slow to_lowercase function?
August 27, 2017 -

I'm trying to implement a simple fuzzy sort library in rust, which I've already wrote a version in JavaScript and I would like to see whether Rust will give a better performance to do that.

Most code's performance showed the power of Rust, but the str::to_lowercase slow down the whole program.

I use the to_lowercase function to case insensitively compare the chars of string. I know using the eq_ignore_ascii_case function will be faster but I still want to consider the non-ASCII characters cases.

So is there any way to get the goal with a better performance? I've tried convert characters into lowercase in the iterator, but the char::to_lowercase return ToLowercase and can not be compared directly.

Below is a bench of str::to_lowercase, did I do anything wrong?

#[bench]
fn lowercase(b: &mut test::Bencher) {
    let target = String::from("Search some long string to test the lowercase benchmark.");
    b.iter(|| target.to_lowercase());
}
Find elsewhere
๐ŸŒ
Rust Internals
internals.rust-lang.org โ€บ libs
Add `as_lowercase` et. al - libs - Rust Internals
December 15, 2021 - There exists a method called str::to_lowercase that allocates a string with unicode case conversion. I propose adding a method called str::as_lowercase with the signature impl str { fn as_lowercase(&self) -> impl Display + '_; } Using this method ...
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-convert-a-character-to-lowercase-in-rust
How to convert a character to lowercase in Rust
We can covert a character to lowercase in Rust by passing the character to to_lowercase() method.
๐ŸŒ
Rust Programming Language
users.rust-lang.org โ€บ t โ€บ char-to-lowercase-problem โ€บ 58196
Char to_lowercase problem - The Rust Programming Language Forum
April 11, 2021 - Hi, I tried to convert a char to lowercase, but compile report a type error, any suggestion? Thanks! use std::collections::BTreeMap; fn main() { let mut new_btm: BTreeMap src\main.rs:5:32 | 5 | let l_c = c.to_lowercase().collect(); | ...
๐ŸŒ
Rust
doc.rust-lang.org โ€บ std โ€บ primitive.str.html
str - Rust
Converts this string to its ASCII lower case equivalent in-place. ASCII letters โ€˜Aโ€™ to โ€˜Zโ€™ are mapped to โ€˜aโ€™ to โ€˜zโ€™, but non-ASCII letters are unchanged. To return a new lowercased value without modifying the existing one, use to_ascii_lowercase().
๐ŸŒ
Rust
docs.rs โ€บ stringcase
stringcase - Rust
Converts the input string to kebab case with the specified options. ... A generic function that converts string cases into a lowercased format joined by a specified joiner character.
๐ŸŒ
Rust
docs.rs โ€บ convert_case
convert_case - Rust
Conversion works on graphemes as defined by the unicode_segmentation library. This means that transforming letters to lowercase or uppercase works on all unicode characters, which also means that the number of characters isnโ€™t necessarily the same after conversion.
๐ŸŒ
Rust FAQ
rustfaq.org โ€บ home โ€บ strings โ€บ how to convert string to uppercase lowercase
How to convert string to uppercase lowercase โ€” Rust FAQ
Convert Rust strings between upper and lower case with to_uppercase/to_lowercase and the ASCII-only fast path, plus the Unicode quirks (รŸ -> SS) you should know about.
๐ŸŒ
Rust
docs.rs โ€บ roe
roe - Rust
This crate provides Unicode case mapping routines and iterators for conventionally UTF-8 binary strings. Unicode case mapping or case conversion can be used to transform the characters in a string.
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ case-transformations-in-rust-strings-uppercase-lowercase-titlecase
Case Transformations in Rust Strings: Uppercase, Lowercase, Titlecase - Sling Academy
January 3, 2025 - The method handles the conversion to lowercase effectively across all characters. Title case conversion is not directly supported by a built-in method in Rust's standard library, but it can be implemented with a few lines of code by capitalizing the first letter of each word in the string.
๐ŸŒ
GitHub
github.com โ€บ rust-lang โ€บ rust โ€บ issues โ€บ 26244
Separate to_lowercase() into correct Unicode and simple implementations ยท Issue #26244 ยท rust-lang/rust
June 12, 2015 - Therefore I suggest splitting this function into two, e.g., to_locale_lowercase(locale) and to_partial_lowercase(): one that fully implements Unicode (requires locale specified and is good for displaying strings to people), and another which is incorrect in many cases, shouldn't be displayed to users, but preserves simple invariants of ASCII lowercasing that make it useful and safe for algorithms that need code-point-wise lowercasing.
Author: rust-lang