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:
- [fix] Java NullPointerException ComparableTimSort countRunAndMakeAscending when sorting a List - Java
- Struts 2 Hello World Example in Eclipse - Java
- Python: How to Plot a Histogram using Matplotlib and data as list - Python
- How to save webpage as pdf using macOS Safari - MacOS
- 'Edit Document' Requires a Windows Sharepoint Services-compatible application and Microsoft Internet Explorer 6.0 or higher - SharePoint
- Microsoft 365 Excel - PRO TIP Discover insights quickly with Power BI, which is already included in your Microsoft services, See it in action - Microsoft
- Android AlertDialog with Yes No and Cancel Button - Android
- Are Windows Notepad files recoverable? - NotepadPlusPlus