0Pricing
Learn Rust Coding · درس

تنظيم الشيفرة باستخدام الوحدات

تعلّموا استخدام نظام الوحدات في Rust لتجميع الشيفرة منطقيًا وإدارة الخصوصية وجعل مشاريعكم قابلة للتوسع.

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

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

Organize Your Code with Modules

As your Rust projects grow, keeping all your code in one file becomes messy. Rust's module system helps you organize code into logical units.

Modules are like folders for your code. They group related functions, structs, enums, and constants together, improving readability and maintainability.

Declaring Your First Module

You declare a module using the mod keyword, followed by the module's name. It's like creating a sub-namespace within your program.

Let's create a simple greetings module:

mod greetings {
    // This is where greeting functions would go
}

fn main() {
    println!("Hello from main!");
}

Understanding Module Privacy

By default, everything inside a module is private. This means you can't access it from outside the module without explicitly making it public.

If you try to call a private function, Rust will give you a compile error. Try uncommenting the line below and running it:

mod greetings {
    fn hello() {
        println!("Hello from the greetings module!");
    }
}

fn main() {
    // Uncomment the line below and try to run!
    // greetings::hello();
    println!("Main function running.");
}

Making Items Public with `pub`

To make an item (like a function, struct, or enum) visible from outside its module, you use the pub keyword.

  • pub fn makes a function public.
  • pub struct makes a struct public.
  • pub mod makes a submodule public, allowing its contents to be accessed.

Privacy is a core part of Rust's safety and organization!

`pub` in Action

Now, let's make our hello function public using pub. This allows the main function to call it without a privacy error.

Run this code to see the greeting from our module:

mod greetings {
    pub fn hello() {
        println!("Hello from the greetings module!");
    }
}

fn main() {
    greetings::hello();
}

Simplifying Paths with `use`

Typing out full paths like greetings::hello() can get repetitive. The use keyword lets you bring items into scope, making them easier to refer to.

It's similar to import in Python or using in C#. You can bring a specific item or an entire module into scope.

`use` in Action

Here's how you can use use to shorten the path to our hello function. Notice how we no longer need the greetings:: prefix.

This makes your code cleaner and easier to read.

mod greetings {
    pub fn hello() {
        println!("Hello, module user!");
    }
}

use greetings::hello; // Bring `hello` into scope

fn main() {
    hello(); // Now we can call it directly
}

Nested Modules & Relative Paths

Modules can be nested! You can define modules inside other modules for even finer organization. For navigating these, Rust provides super and self.

  • super refers to the parent module.
  • self refers to the current module.

These are useful for relative paths within complex module hierarchies.

mod outer {
    pub mod inner {
        pub fn call_me() {
            println!("Called from inner module!");
        }
    }

    pub fn call_inner() {
        // Accessing inner module from parent
        inner::call_me();
    }
}

fn main() {
    outer::call_inner();
    outer::inner::call_me();
}

Modules and File Structure

For larger modules, you can put their code into separate files. Rust automatically looks for module definitions in specific locations:

  • For mod my_module; in src/main.rs: Rust looks for src/my_module.rs or src/my_module/mod.rs.
  • This helps keep your main file clean and delegates content to appropriate files.

This lesson uses single files for simplicity, but remember this for bigger projects!

Module Access Check

Consider the following Rust code. Which lines will compile without a privacy error?

mod calculator {
    fn add(a: i32, b: i32) -> i32 {
        a + b
    }

    pub fn subtract(a: i32, b: i32) -> i32 {
        a - b
    }

    mod advanced {
        pub fn multiply(a: i32, b: i32) -> i32 {
            a * b
        }
    }
}

fn main() {
    // Line A
    // calculator::add(5, 3);

    // Line B
    // calculator::subtract(10, 4);

    // Line C
    // calculator::advanced::multiply(2, 6);

    // Line D
    // calculator::advanced::add(1, 1);
}

Modules: Organize & Control

Great job! You've learned how Rust's module system helps you structure your code:

  • Use mod to declare modules and submodules.
  • Items are private by default; use pub to make them accessible.
  • use simplifies long paths to items.
  • Modules can be spread across multiple files for large projects.

Modules are fundamental for managing complexity in Rust. Keep practicing!

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

هل درس «تنظيم الشيفرة باستخدام الوحدات» مجاني؟

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

ماذا ستتعلم في «تنظيم الشيفرة باستخدام الوحدات»؟

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

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

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

كم من الوقت يستغرق درس «تنظيم الشيفرة باستخدام الوحدات»؟

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

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

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

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

  1. تنظيم الشيفرة باستخدام الوحدات
  2. إدارة الاعتماديات باستخدام Crates
  3. معالجة الأخطاء المتينة باستخدام `Result`
← العودة إلى Learn Rust Coding