The length of a string is the number of characters that make up that string. We can get the length of a string in Rust using the len()
method.
string.len()
string
: This is the string whose length we want to get.The value returned is a number representing the length of the string specified.
fn main() { // create some strings let str1 = "Edpresso"; let str2 = "Educative"; let str3 = "Rust"; let str4 = "Educative is the best platform!"; // print the length of the strings println!("The length of {} is {}", str1, str1.len()); println!("The length of {} is {}", str2, str2.len()); println!("The length of {} is {}", str3, str3.len()); println!("The length of {} is {}", str4, str4.len()); }
In the above code:
len()
method, and with the println!()
method, we print the result to the console.RELATED TAGS
CONTRIBUTOR
View all Courses