본문 바로가기
Personal-Study/Rust

[Rust 문서 읽기] 8. Common Collections (일반적인 컬렉션)

by Aaron-Kim 2023. 2. 11.

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


Common Collections (영어)

일반적인 컬렉션 (한국어)

반응형

댓글