The Rust Programming Language
- Storing Lists of Values with Vectors
- A vector allows us to store a variable number of values next to each other
- only store values of the same type
- useful such as the lines of text in a file or the prices of items in a shopping cart
- Creating a New Vector
- we add a type annotation b/c when aren't inserting any values into vector (rarely)
- vec! macro: create a new vector that holds the values we give it
- Updating a Vector
- push method (with mut keyword on variable declaration)
- Reading Elements of Vectors
- indexing
- useful when we want our program to crash
if there's an attempt to access an element past the end of the vector
- get method
- returns Option<&T>
- Iterating over the Values in a Vector
- for .. in ..
- to change the value that the mutable reference, we have to use the * dereference operator
- the reference to the vector that the for loop
holds prevents simultaneous modification of the whole vector
- Using an Enum to Store Multiple Types
- Dropping a Vector Drops Its Elements
- when the vector gets dropped, all of its contents are also dropped
- Storing UTF-8 Encoded Text with Strings
- A string is a collection of characters
- strings are implemented as a collection of bytes
- What is a String?
- str (&str)
- String
- Creating a New String
- String::new()
- to_string()
- String::from()
- Updating a String
- Appending to a String with push_str and push
- push_str
- push (single character only)
- Concatenation with the + Operator for the format! Macro
- + operator
- uses the add method
- compiler coerce the &String argument into a &str
- takes ownership in front of the parameter
- format! macro
- much easier to read
- uses references so that this call doesn't take ownership of any of its parameters
- Indexing into Strings
- Internal Representation
- A String is a wrapper over a Vec<u8>
- encode to UTF-8
- Bytes and Scalar Values and Grapheme Clusters! Oh My!
- Slicing Strings
- [] (range)
- Methods for Iterating Over Strings
- chars() method (char)
- bytes() method (bytes)
- Strings Are Not So Simple
- Storing Keys with Associated Values In Hash Maps
- A hash map allows us to associate a value with a particular key
- HashMap<K, V>
- homogemeous (all of the keys, all of the values - type same as each other)
- Creating a New Hash Map
- new
- insert
- Accessing Values in a Hash Map
- get method
- returns Option<&V>
- Hash Maps and Ownership
- Updating a Hash Map
- Overwriting a Value
- same key -> the value associated with key will be replaced
- Adding a Key and Value Only If a Key Isn't Present
- Entry (enum type)
- entry method with or_insert method
- Updating a Value Based on the Old Value
- Hashing Functions
- Summary
'Personal-Study > Rust' 카테고리의 다른 글
[Rust 문서 읽기] 9. Error Handling (예외 처리) (0) | 2023.02.18 |
---|---|
[Rust 문서 읽기] 7. Managing Growing Projects with Packages, Crates, and Modules (모듈) (0) | 2023.02.11 |
[Rust 문서 읽기] 6. Enums and Pattern Matching (열거형과 패턴 매칭) (0) | 2023.02.05 |
[Rust 문서 읽기] 5. Using Structs to Structure Related Data (연관된 데이터들을 구조체로 다루기) (0) | 2023.02.05 |
[Rust 문서 읽기] 4. Understanding Ownership (소유권 이해하기) (0) | 2023.01.29 |
댓글