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
![How to Split a String in Rust How to Split a String in Rust](https://code2care.org/q/split-a-string-by-space-in-rust/images/How to Split a String in Rust.jpg)
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:
- Use your iPhone microphone as a mic on macOS Ventura - MacOS
- Adding Custom ASCII Text Banner in Spring Boot Application - Java
- Java C# foreach loop equivalent in Python Programming - Python
- Users experience call quality issue, voice distortion, disconnection with Microsoft Teams call and meeting - Teams
- How to come out of Full Screen Mode Roblox on Mac - MacOS
- Show CPU and Memory Usage on macOS Menu Bar - MacOS
- How to clear MySQL Console Screen - MySQL
- How to know the MAC address of iPhone/iPad Wifi - iOS