Why do assert statements need explicit & for literal text?


   let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];

    let mut my_iterable_fav_fruits = my_fav_fruits.iter();

    assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));

In the creation of my_fav fruits, the literal text of banana etc is represented as a &str' which is what I would expect. My understanding is that all literal text is represented/interpreted by Rust as a &str.

Yet, the literal text of "banana" in the assert statement does not seem to be interpreted as a &str but as just str. Indeed, it won't compile without the "&".

At first I thought I had overlooked something specific with Option, but if I create a Some on its own, like this:

let t = Some("foo");

t is a &str... So it's apparently not something unique to Option.


No, it's &str. But my_iterable_fav_fruits.next() yields not &str, but &&str, because iterator, returned by iter(), yields references to the original elements.