book
book copied to clipboard
Chapter 8: ways to initialize new Vector and String
Vectors and Strings were discussed in parallel in Chapter 8. This thread aims at improving the completeness and consistency of the presented materials.
Current flow of ideas:
- Vector: [Section 8.1]
- Create a new vector using
Vec::new() - Create a new vector with initial value using
a)
vec!
- Create a new vector using
- String: [Section 8.2]
- Create a new string using
String::new() - Create a new string with initial value using:
a)
to_string()b)String::from()
- Create a new string using
Suggested flow of ideas:
- Vector: [Section 8.1]
- Create a new vector using
Vec::new() - Create a new vector with initial value using:
a)
Vec::from()b)vec!
- Create a new vector using
- String: [Section 8.2]
- Create a new string using
String::new() - Create a new string with initial value using:
a)
String::from()b)" ".to_string()
- Create a new string using
The rational behind this modification is:
Vec::from()is analogous toString::from()and has been supported since version 1.44.- It might be better to introduce
String::from()first because- it belongs to the
Stringtype as opposed toto_stringwhich belongs to the other types such asstr - plus
String:from()was used in previous chapters
- it belongs to the