rust

Why can the argument of a function implicitly change its type from &&T to &T?


Consider the following example, the test function receives the &str type, but it works when I transfer it &&str, &&&str, &&&&str. What's the rule? I don't think it is the deref coercion, because Rust std does not implement Deref for &&str, &&&str.

fn main() {
    let x = String::new();
    let y: &str = &x;
    let z: &&&&str = &&&y;
    
    test(z);
}

fn test(x: &str) {
}

Solution

  • Rust std does not implement deref for &&str, &&&str.

    Why do you think that?

    std implements Deref for &T. &&&str is an &T where T = &&str. So std implements Deref for &&&str. And &&str.