To get all possible substrings of a string in Rust, you can use nested loops to iterate through each combination of start and end indices.
In this example,
str
with a value of 'Hello'
.substrings
to store all substrings.substrings
vector.substrings
vector.fn main() {
let str = "Hello";
let mut substrings = Vec::new();
for i in 0..str.len() {
for j in i + 1..=str.len() {
substrings.push(&str[i..j]);
}
}
println!("All substrings: {:?}", substrings);
}
All substrings: ["H", "He", "Hel", "Hell", "Hello", "e", "el", "ell", "ello", "l", "ll", "llo", "lo", "o"]
In this tutorial, we learned How to get all the possible Substrings of a String in Rust language with well detailed examples.