Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let s = String::from("hello world");
let a = &s[0..5];
let b = &s[6..];
println!("{b} {a}");
}world hello
- Expecting
world hell:a..bincludesaand stops just beforeb, so0..5takes indices 0 to 4, all five letters ofhello. - Expecting a compile error for two slices of one
String: both are shared borrows, and the rules allow any number of&at once.
- What would
&s[0..=5]contain, and how would the output change? - Why does
&s[0..1]panic whensholds Cyrillic text?