Rust の参照カウントメカニズムを探る

Beginner

This tutorial is from open-source community. Access the source code

はじめに

この実験では、Rust におけるRc(参照カウント)の使い方を調べます。これは、参照の数を追跡することで値の所有権を管理することができます。

注: 実験でファイル名が指定されていない場合は、好きなファイル名を使用できます。たとえば、main.rsを使用して、rustc main.rs &&./main でコンパイルして実行することができます。

Rc

複数の所有権が必要な場合、Rc(参照カウント)を使用できます。Rcは参照の数を追跡します。これは、Rcの中にラップされた値の所有者の数を意味します。

Rcがクローンされるたびに、Rcの参照カウントは 1 増加し、クローンされたRcの 1 つがスコープ外になるたびに 1 減少します。Rcの参照カウントがゼロになると(所有者がいなくなることを意味します)、Rcと値の両方が破棄されます。

Rcをクローンすると、深いコピーは行われません。クローンは、ラップされた値への別のポインタを作成し、カウントを増やします。

use std::rc::Rc;

fn main() {
    let rc_examples = "Rc examples".to_string();
    {
        println!("--- rc_a is created ---");

        let rc_a: Rc<String> = Rc::new(rc_examples);
        println!("Reference Count of rc_a: {}", Rc::strong_count(&rc_a));

        {
            println!("--- rc_a is cloned to rc_b ---");

            let rc_b: Rc<String> = Rc::clone(&rc_a);
            println!("Reference Count of rc_b: {}", Rc::strong_count(&rc_b));
            println!("Reference Count of rc_a: {}", Rc::strong_count(&rc_a));

            // Two `Rc`s are equal if their inner values are equal
            println!("rc_a and rc_b are equal: {}", rc_a.eq(&rc_b));

            // We can use methods of a value directly
            println!("Length of the value inside rc_a: {}", rc_a.len());
            println!("Value of rc_b: {}", rc_b);

            println!("--- rc_b is dropped out of scope ---");
        }

        println!("Reference Count of rc_a: {}", Rc::strong_count(&rc_a));

        println!("--- rc_a is dropped out of scope ---");
    }

    // Error! `rc_examples` already moved into `rc_a`
    // And when `rc_a` is dropped, `rc_examples` is dropped together
    // println!("rc_examples: {}", rc_examples);
    // TODO ^ Try uncommenting this line
}

まとめ

おめでとうございます!あなたはRcの実験を完了しました。あなたの技術を向上させるために、LabEx でさらに多くの実験を行って練習してください。