rustHow do I split strings in Rust?
Strings in Rust can be split using the split() method. This method takes a string and a delimiter as parameters and returns an iterator of strings.
Example code
let my_string = "Hello, World!";
let split_string = my_string.split(",");
Output example
["Hello", " World!"]
Code explanation
let my_string = "Hello, World!";: This line declares a string variable calledmy_stringand assigns it the value"Hello, World!".let split_string = my_string.split(",");: This line calls thesplit()method on themy_stringvariable, passing in the delimiter","as a parameter. This returns an iterator of strings.
Helpful links
More of Rust
- Rust map function example
- How to match whitespace with a regex in Rust?
- How to use regex lookbehind in Rust?
- How to use regex to match a group in Rust?
- How to insert an element into a Rust HashMap if it does not already exist?
- Yield example in Rust
- How to compare two Rust HashMaps?
- How to map a Rust slice?
- How to modify an existing entry in a Rust HashMap?
- How to use Unicode in a regex in Rust?
See more codes...