0Pricing
Learn Rust Coding · درس

pub وإمكانية الوصول

التحكم في الوصول

pub وإمكانية الوصول درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Private by Default

In Rust, everything inside a module is private by default. Other modules cannot see it unless you opt in with pub.

This safe default keeps implementation details hidden until you deliberately expose them.

pub Functions

Marking a function pub makes it callable from outside its module. Without it, the call fails to compile.

mod math {
    pub fn square(x: i32) -> i32 {
        x * x
    }
}

fn main() {
    println!("{}", math::square(5));
}

Private Helpers Stay Hidden

You can keep internal helpers private while exposing a public API. The public function may call the private one, but outsiders cannot.

mod math {
    fn double(x: i32) -> i32 { x * 2 }
    pub fn quadruple(x: i32) -> i32 { double(double(x)) }
}

fn main() {
    println!("{}", math::quadruple(3));
}

pub Structs Hide Fields

Making a struct pub exposes the type, but its fields stay private unless each is also marked pub.

This lets you control how the struct is created and modified.

mod model {
    pub struct User {
        pub name: String,
        age: u32,
    }
    impl User {
        pub fn new(name: String, age: u32) -> User {
            User { name, age }
        }
        pub fn age(&self) -> u32 { self.age }
    }
}

fn main() {
    let u = model::User::new(String::from("Sam"), 28);
    println!("{} is {}", u.name, u.age());
}

Why Hide Fields?

Private fields enforce invariants. Callers must use your constructor and methods, so you can validate inputs and change the internal layout later without breaking users.

pub Enums Expose Everything

Unlike structs, a pub enum makes all its variants public automatically. Variants have no separate visibility.

mod traffic {
    pub enum Light {
        Red,
        Yellow,
        Green,
    }
}

fn main() {
    let l = traffic::Light::Green;
    let go = matches!(l, traffic::Light::Green);
    println!("go = {}", go);
}

pub(crate): Crate-Wide

pub(crate) makes an item visible everywhere in the same crate, but not to outside crates that depend on yours.

It is perfect for internal APIs shared across modules.

mod config {
    pub(crate) fn default_port() -> u16 {
        8080
    }
}

fn main() {
    println!("{}", config::default_port());
}

pub(super): Parent-Only

pub(super) exposes an item only to the parent module. It is a narrower grant than full pub.

mod outer {
    pub fn run() {
        inner::helper();
    }
    mod inner {
        pub(super) fn helper() {
            println!("visible to parent only");
        }
    }
}

fn main() {
    outer::run();
}

Re-exporting With pub use

pub use re-exports an item, letting callers reach it through a shorter or more convenient path. This shapes a clean public API.

mod deep {
    pub mod nested {
        pub fn ping() {
            println!("pong");
        }
    }
    pub use nested::ping;
}

fn main() {
    deep::ping();
}

Designing a Public API

Expose the smallest surface that gets the job done. Keep helpers private, hide struct fields behind methods, and use pub(crate) for internal sharing.

A tight API is easier to evolve without breaking users.

Visibility Levels Summary

From most to least restrictive:

  • default (private to the module)
  • pub(super) — visible to the parent
  • pub(crate) — visible across the crate
  • pub — visible everywhere

Quick Check

Test your grasp of visibility.

Recap

You learned to control access:

  • Items are private by default; pub exposes them
  • pub structs keep fields private unless each is pub
  • pub enums expose all variants
  • pub(crate) and pub(super) give finer control
  • pub use re-exports to shape a clean API

الأسئلة الشائعة

هل درس «pub وإمكانية الوصول» مجاني؟

نعم — نص درس «pub وإمكانية الوصول» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.

ماذا ستتعلم في «pub وإمكانية الوصول»؟

التحكم في الوصول تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟

لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «pub وإمكانية الوصول»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟

نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. الوحدات وmod
  2. pub وإمكانية الوصول
  3. use والمسارات
  4. Crates وجذر Crate
← العودة إلى Learn Rust Coding