6

I'm trying to understand the concept of strings and string slices.

fn say_hello_slice(slice: &str) {
    println!("Hey {}", slice);
}

fn say_hello_string(string: &String) {
    println!("{:?}", string);
}

fn print_int(int_ref: &i32) {
    println!("{:?}", int_ref);
}

fn main() {
    let slice: &str = "you";
    let s: String = String::from("String");
    say_hello_slice(slice);
    say_hello_slice(&s);

    let number: i32 = 12345;
    print_int(&number);

    say_hello_string(&s);
}

This program gives the below output when I compile and run:

Hey you
Hey String
12345
"String"

I understand that when & is added to the binding it becomes a reference to its binding type. For example, & to number in above program becomes &i32.

I don't understand how it works when I add & to String and it becomes &str.

user51
  • 8,843
  • 21
  • 79
  • 158
  • 2
    Possible duplicate of [What are the differences between Rust's \`String\` and \`str\`?](https://stackoverflow.com/questions/24158114/what-are-the-differences-between-rusts-string-and-str) – ljedrz Mar 16 '18 at 07:43

1 Answers1

11

You have just witnessed the Deref trait. This trait has three uses:

  • Conversion to another type when dereferencing (the * operator)
  • Automatic conversion if necessary (coercion) when borrowing
  • You can call methods of another type directly.

In your case, because String implements Deref<Target = str> this means that &s is coerced into a &str.

More info here.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Minijackson
  • 146
  • 2
  • 5