Vectors

If you remember, array is a fixed-size list of elements, of same data type. Even with mut , it’s element count can not be changed. Vector is kind of a re-sizable array but all elements must be in the same type.

⭐️ It is a generic type, written as Vec<T> . T can have any type, ex. The type of a Vec of i32s is Vec<i32> . Also Vectors always allocate their data in dynamically allocated heap.

⭐️ Mainly a vector represents 3 things; a pointer to the data, No of elements currently have(length), capacity (Amount of space allocated for any future elements). If the length of a vector exceeds its capacity, its capacity will be increased automatically. But its elements will be reallocated(which can be slow). So always use Vec::with_capacity whenever it’s possible.

🔎 String data type is a UTF-8 encoded vector. But you can not index into a String because of encoding.

💯 Vectors can be used with iterators in three ways,