Rust構造体フィールドの可視性

RustRustBeginner
今すぐ練習

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

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、Rust における構造体の可視性と、それがフィールドのアクセシビリティにどのように影響するかについて学びます。

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL rust(("Rust")) -.-> rust/AdvancedTopicsGroup(["Advanced Topics"]) rust(("Rust")) -.-> rust/BasicConceptsGroup(["Basic Concepts"]) rust(("Rust")) -.-> rust/DataTypesGroup(["Data Types"]) rust(("Rust")) -.-> rust/FunctionsandClosuresGroup(["Functions and Closures"]) rust(("Rust")) -.-> rust/DataStructuresandEnumsGroup(["Data Structures and Enums"]) rust/BasicConceptsGroup -.-> rust/variable_declarations("Variable Declarations") rust/DataTypesGroup -.-> rust/string_type("String Type") rust/DataTypesGroup -.-> rust/type_casting("Type Conversion and Casting") rust/FunctionsandClosuresGroup -.-> rust/function_syntax("Function Syntax") rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("Expressions and Statements") rust/DataStructuresandEnumsGroup -.-> rust/method_syntax("Method Syntax") rust/AdvancedTopicsGroup -.-> rust/operator_overloading("Traits for Operator Overloading") subgraph Lab Skills rust/variable_declarations -.-> lab-99333{{"Rust構造体フィールドの可視性"}} rust/string_type -.-> lab-99333{{"Rust構造体フィールドの可視性"}} rust/type_casting -.-> lab-99333{{"Rust構造体フィールドの可視性"}} rust/function_syntax -.-> lab-99333{{"Rust構造体フィールドの可視性"}} rust/expressions_statements -.-> lab-99333{{"Rust構造体フィールドの可視性"}} rust/method_syntax -.-> lab-99333{{"Rust構造体フィールドの可視性"}} rust/operator_overloading -.-> lab-99333{{"Rust構造体フィールドの可視性"}} end

構造体の可視性

構造体は、そのフィールドに関して追加の可視性レベルを持っています。可視性はデフォルトで非公開になっており、pub 修飾子で上書きすることができます。この可視性は、構造体が定義されているモジュールの外部からアクセスされる場合にのみ重要であり、情報を隠す(カプセル化)目的があります。

mod my {
    // ジェネリック型 `T` のパブリックフィールドを持つパブリック構造体
    pub struct OpenBox<T> {
        pub contents: T,
    }

    // ジェネリック型 `T` のプライベートフィールドを持つパブリック構造体
    pub struct ClosedBox<T> {
        contents: T,
    }

    impl<T> ClosedBox<T> {
        // パブリックコンストラクタメソッド
        pub fn new(contents: T) -> ClosedBox<T> {
            ClosedBox {
                contents: contents,
            }
        }
    }
}

fn main() {
    // パブリックフィールドを持つパブリック構造体は通常通り構築できます
    let open_box = my::OpenBox { contents: "public information" };

    // そしてそのフィールドに通常アクセスできます。
    println!("The open box contains: {}", open_box.contents);

    // プライベートフィールドを持つパブリック構造体は、フィールド名を使って構築できません。
    // エラー! `ClosedBox` はプライベートフィールドを持っています
    //let closed_box = my::ClosedBox { contents: "classified information" };
    // TODO ^ この行のコメントを外してみてください

    // ただし、プライベートフィールドを持つ構造体は、
    // パブリックコンストラクタを使って作成できます
    let _closed_box = my::ClosedBox::new("classified information");

    // そして、パブリック構造体のプライベートフィールドにはアクセスできません。
    // エラー! `contents` フィールドはプライベートです
    //println!("The closed box contains: {}", _closed_box.contents);
    // TODO ^ この行のコメントを外してみてください
}

まとめ

おめでとうございます!あなたは構造体の可視性の実験を完了しました。あなたのスキルを向上させるために、LabExでさらに多くの実験を行って練習してください。