We can split a String in Rust based on spaces by making use of the split_whitespace() method.
The split_whitespace() method returns an iterator which can be collect as Vec<&str> using the collect() method.
Example:fn main() {
let data_string = "This is my String that I want to Split";
let splitted_str: Vec<&str> = data_string.split_whitespace().collect();
for word in splitted_str {
println!("{}", word);
}
}
Output:
This
is
my
String
that
I
want
to
Split

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Rust,
- Rust: Cargo Init vs Cargo New Command
- Rust: Write and Run Hello World! Program Example
- How to Split a String using Rust Language
- How to Sort a Vector in Rust with Examples
- Fix: error: could not find `Cargo.toml` in Users or any parent directory
- How to uninstall Rust Language from Mac/Linux/Ubuntu
- How to update Cargo (Rust Lang)
- Fix: rust-analyzer failed to discover workspace [Visual Studio Code]
- How to install Rust using rustup on macOS/Linux/Ubuntu
- Cargo Watch: To Recompile Rust Project Automatically
- Difference between rustc and cargo build commands
- How to Split a String by Space in Rust
- How to know Rust is Installed on Mac?
- Rust: zsh: no such file or directory: ./main
- How to update Rust on Mac/Linux
- List of Rust Cargo Commands
- How to find version of Cargo in Rust
- Fix: error: mismatched closing delimiter } [Rust]
More Posts:
- How to Quit Applications on Mac Using Terminal - MacOS
- Quick way to setup AWS DynamoDB locally on macOS - AWS
- FCM Messages Test Notification!!!! - Microsoft Teams, Google Hangouts push alert - News
- Hide Navigation Bar from Android Screen Activity - Android
- Create SharePoint Site Collection with new Content database in existing web application - SharePoint
- How to install Python on Ubuntu - Ubuntu
- Notepad++ Export file as HTML - NotepadPlusPlus
- How to install Python Specific version (3.8, 3.9 or 3.10) using Brew - Python