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!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
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:
- java.util.Optional Class Deep Dive Tutorial - Java
- Get the total size and number of objects of a AWS S3 bucket and folders - AWS
- Bash How to Save Output of a Command to a Variable - Bash
- Stop android adb service from command prompt or terminal - Android
- How to fix Java nio NoSuchFileException wile reading a file - Java
- How to Turn on Light/Dark Mode on Xcode IDE - MacOS
- How to know the Size of an Object in Python - Python
- 16: Find the largest element in a List - 1000+ Python Programs - Python-Programs