The split_whitespace()
method of a string in Rust splits a string into sub-strings at whitespace. This means it will slice the string into multiple sub-strings at every whitespace.
string.split_whitespace()
string
: This is the string we want to split into sub-strings.
The value returned is an iterator containing the sliced sub-strings.
fn main() {// create some stringslet str1 = "Rust is very interesting";let str2 = "Educative is the best";// print substrings of stringprintln!("The substrings without whitespaces in '{}' are:", str1);for byte in str1.split_whitespace() {println!("{}", byte);}// print substrings of stringprintln!("\nThe substrings without whitespaces in '{}' are:", str2);for byte in str2.split_whitespace() {println!("{}", byte);}}
split_whitespace()
method. We print each string using the for in
loop.