To check if a string is empty in Rust, you can use the is_empty()
method of the String type.
In this example,
str1
with an empty string value ""
.is_empty()
method on str1
to check if it is empty.is_empty()
method returns true
if the string is empty, otherwise false
.fn main() {
let str1 = String::new();
let is_empty = str1.is_empty();
println!("Is string empty: {}", is_empty);
}
Is string empty: true
In this example,
str2
with a non-empty string value "Hello World"
.is_empty()
method on str2
to check if it is empty.is_empty()
method returns true
if the string is empty, otherwise false
.fn main() {
let str2 = String::from("Hello World");
let is_empty = str2.is_empty();
println!("Is string empty: {}", is_empty);
}
Is string empty: false
In this tutorial, we learned How to Check if String is Empty in Rust language with well detailed examples.