To split a string into two strings in Rust, you can use the split_once
method available since Rust 1.49. This method returns an optional tuple where the first element is the string up to the first occurrence of the delimiter, and the second element is the rest of the string after the delimiter.
Here's an example code snippet:
main.rs319 chars13 lines
In this example, my_string
is split into two strings at the first occurrence of ", "
. The output of the program is:
main.rs36 chars3 lines
Note that the split_once
method returns an Option
since the delimiter might not be found in the string. In this case, the None
branch of the match statement is executed.
gistlibby LogSnag