Passing vector by reference vs passing a slice to a function

Re dereferences, graphically the difference between &[T] and &Vec<T> is[1]

+---+---+---+---+---+---+---+---+
| Pointer       | Length        | &[T] (or &str)
+---+---+---+---+---+---+---+---+
  |
  V
+---+---+---+---+---+---+---+---+
| D | A | T | A | . | . | . | ......    [T] (or str)
+---+---+---+---+---+---+---+---+   
  ^
  |
+---+---+---+---+---+---+---+---+---+---+---+---+
| Pointer       | Length        | Capacity      | Vec<T> (or String)
+---+---+---+---+---+---+---+---+---+---+---+---+
  ^
  |
+---+---+---+---+
| Pointer       | &Vec<T> (or &String)
+---+---+---+---+

  1. order of fields is not guaranteed ↩︎

9 Likes