본문 바로가기
Personal-Study/Rust

[Rust 문서 읽기] 4. Understanding Ownership (소유권 이해하기)

by Aaron-Kim 2023. 1. 29.

The Rust Programming Language

- Ownership concept enables Rust to make memory safety guarantees without needing a G.C
- What is Ownership?

  - Ownership is a set of rules that govern how a Rust program manages memory

  - memory is managed through a system of ownership with a set of rules that the compiler checks

  - None of the features of ownership will slow down your program while it's running

  - keeping track of what parts of code are using what data on the heap,
     minimizing the amount of duplicate data on the heap, and cleaning up unused data on the heap
     so we don't run out of spaces thanks to Ownership addresses

  - the main purpose of ownership is to manage heap data more efficient and fast

  - Stack & Heap

    - Stack

      - All data stored on the stack must have a known, fixed size

      - Pushing to the stack is faster than allocating on the heap

         (b/c the allocator (OS) never has to search for a place to store new data)

    - Heap

      - Data with an unknown size at compile time or a size that might change must be stored on the heap

      - memory allocator

  - Ownership Rules

    - Each value in Rust has an owner

    - There can only be one owner at a time

    - When the owner goes out of scope, the value will be dropped

  - Variable Scope

    - Scope: the range within a program for which an item is valid

  - The String Type

    - manages data allocated on the heap
       and as such is able to store an amount ot text that is unknown to us at compile time

  - Memory and Allocation

    - The memory must be requested from the memory allocator at runtime

    - We need a way of returning this memory to the allocator when we're done with our String

      - the memory is automatically returned once the variable that owns it goes out of scope

      - when a variable goes out of scope, Rust calls drop function
        (automatically at the closing curly bracket)

    - Variable and Data Interacting with Move

      - A String is made up of three parts

        - a pointer to the memory that holds the contents of the string

        - a length (memory used)

        - a capacity (memory allocated)

      - copy the pointer, length, and capacity on to the stack, not copy data on the heap

      - not just a shallow copy, invalidate the previous variable (called "move")

      - Rust will never automatically create "deep copy" of our data

    - Variables and Data Interacting with Clone

      - if we do want to a deeply copy, not just on the stack data
         we can use a method called clone

      - copy the real heap data

    - Stack-Only Data: Copy

      - Copy trait (special annotation)

      - Rust won't let us annotate a type with Copy
        if the type, or any of its parts, has implemented the Drop trait

      - Some of the types that implement Copy

        - All the integer types

        - The Boolean type

        - All the floating-point types

        - The character type

        - Tuples, if they only contain types that also implement Copy

  - Ownership and Functions

  - Return Values and Scope

- References and Borrowing

  - A Reference is like a pointer in that it's an address
    we can follow to access the data stored at that address

    (Unlike a pointer, a reference is guaranteed to point to a valid value
      of a particular type for the life of that reference)

    - ampersands represent references,
       and they allow us to refer to some value without taking ownership of it

    - dereferencing => * operator

    - the signature of the function uses & to indicate that the type of the paramter is a reference

    - references are immutable by default
      if want to mutate, just make it to &mut

    - the action of creating a referencing => borrowing

  - Mutable References

    - &mut

    - we can mutate the borrowing value

    - restriction: only one references to that value, not multiple

       => Rust can prevent data races at compile time

    - combining mutable and immutable references -> error

    - multiple immutable references are Ok

    - a reference's scope starts from where it is introduced and
      continues through the last time that reference is used

  - Dangling References

    - the compiler guarantees that references will never be dangling references

  - The Rules of References

    - We can have either one mutable reference or any number of immutable references

    - References must always be valid

- The Slice Type

  - A slice is kind of reference, so it does not have ownership

    - enumerate method returns a tuple

  - String Slices

    - A reference to part of a String

      - the type that signifies "string slice" is written as &str

      - string literals are string slices already

      - &str is an immutable reference

    - String Literals as Slices

    - String Slices as Parameters

  - Other Slices

  - Summary

    - The concepts of ownership, borrowing, and slices ensure memory safety
       in Rust programs at compile time


Understanding Ownership (영어)

소유권 이해하기 (한국어)

반응형

댓글