Back to questions
JuniorTheoryCommon
What does the program print?
fn tail(n: usize, s: &str) -> &str {
&s[n..]
}
fn main() {
let s = String::from("hello");
println!("{}", tail(2, &s));
}llo
- Expecting E0106 because
tailhas two parameters: elision rules count only references, andn: usizeis not one, so rule 2 givess's lifetime to the result. - Reading
&s[n..]as the firstnbytes: the rangen..starts at indexnand runs to the end.
- What does
taillook like with the lifetime the compiler fills in written out? - Why would
fn pick(x: &str, y: &str) -> &strnot compile without annotations?