Skip to main content

How to Check if a String Is Empty in Rust

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

is_empty() tells you whether a Rust string has nothing in it. A string of spaces isn't empty, so to treat blank text as empty, trim it first: text.trim().is_empty().

Syntax​

Syntax
<text>.is_empty()
<text>.trim().is_empty()
PartWhat it does
<text>A &str or a String.
.is_empty()true when the text has a length of zero, otherwise false.
.trim()The text without leading and trailing whitespace, so .trim().is_empty() is also true for blank text.

The standard library's documentation for str::is_empty: "Returns true if self has a length of zero bytes." str::trim "Returns a string slice with leading and trailing whitespace removed."

The Example​

The example checks three strings: "Ada", an empty string, and three spaces, which it checks once more after trimming.

Prerequisites​

  • Rust, installed with rustup, which comes with Cargo. This guide uses Rust 1.100.0.

Step 1: Create a Project​

Terminal
cargo new demo
cd demo
Output
Creating binary (application) `demo` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

cargo new demo creates a folder named demo with a Cargo.toml file, which describes the project, and src/main.rs, which holds the code. cd demo moves into it.

Step 2: Write the Code​

Replace the contents of src/main.rs with:

src/main.rs, which prints is_empty for "Ada", for an empty string, for three spaces, and for three spaces after trim.

src/main.rs
fn main() {
println!("{}", "Ada".is_empty());
println!("{}", "".is_empty());
println!("{}", " ".is_empty());
println!("{}", " ".trim().is_empty());
}

Step 3: Run It​

Terminal
cargo run -q
Output
false
true
false
true

A zsh terminal where cargo run -q prints false, true, false and true on four lines.

cargo run compiles the program and runs it, and -q hides Cargo's own status lines. Only the empty string is empty. Three spaces are three bytes, so is_empty() says false, until trim() removes them.

On a String​

is_empty() works the same on a String. A new, empty String and a String of three spaces gave:

src/main.rs
fn main() {
let name = String::new();
println!("{} {}", name.is_empty(), name.len());
let t = String::from(" ");
println!("{} {}", t.is_empty(), t.len());
}
Output
true 0
false 3

len() counts bytes, and is_empty() is true exactly when len() is 0.

Common Mistakes​

MistakeWhat happensDo this instead
Using is_empty() to reject blank input" " passes as not emptyCheck text.trim().is_empty()
Comparing with ""name == "" works, but reads less clearlyUse name.is_empty()
Expecting trim() to change the original texttrim() returns a new string slice; the original keeps its spacesUse the slice trim() returns

Troubleshooting​

SymptomLikely causeFix
Text that looks empty isn'tIt holds spaces, tabs or a newlineTrim it before checking
A form field with only spaces gets throughThe check uses is_empty() aloneUse trim().is_empty()

FAQ​

is_empty() or len() == 0?​

They give the same answer, and is_empty() says what you mean.

Does trim remove newlines too?​

Yes. The documentation says whitespace "is defined according to the terms of the Unicode Derived Core Property White_Space, which includes newlines."

Conclusion​

text.is_empty() is true only for text with zero bytes. Add trim() first, text.trim().is_empty(), when spaces alone should count as empty.

Additional Resources​