الوحدات وmod
تنظيم الشيفرة
الوحدات وmod درس مجاني في Learn Rust Coding على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Learn Rust Coding، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Modules?
As programs grow, putting everything in one file becomes messy. Modules let you group related code, control what is public, and avoid name clashes.
The mod keyword declares a module.
Declaring an Inline Module
You can define a module right inside a file with mod name { ... }. Everything between the braces belongs to that module.
mod greetings {
fn hello() {
println!("hello from the module");
}
}
fn main() {
println!("module declared above");
}Calling Module Functions
Items inside a module are private by default. To call one from outside, it must be pub, and you reference it with the module path using ::.
mod greetings {
pub fn hello() {
println!("hello!");
}
}
fn main() {
greetings::hello();
}Nested Modules
Modules can contain other modules, forming a tree. The path lists each level separated by ::.
mod app {
pub mod ui {
pub fn draw() {
println!("drawing ui");
}
}
}
fn main() {
app::ui::draw();
}Modules Group More Than Functions
A module can hold functions, structs, enums, constants, and even other modules. This keeps related types and behavior together.
mod shapes {
pub struct Circle {
pub radius: f64,
}
pub const PI: f64 = 3.14159;
}
fn main() {
let c = shapes::Circle { radius: 2.0 };
println!("area = {}", shapes::PI * c.radius * c.radius);
}The Module Tree
Every crate has a root module. Modules you declare form a tree beneath it. Paths describe where an item lives, similar to folders in a file system.
super: The Parent Module
Inside a module, super refers to the parent module. It lets a child call something defined one level up.
fn top_level() {
println!("called via super");
}
mod inner {
pub fn run() {
super::top_level();
}
}
fn main() {
inner::run();
}self: The Current Module
self refers to the current module. It is mostly used to be explicit about where an item lives within a path.
mod tools {
pub fn helper() {
println!("helper running");
}
pub fn run() {
self::helper();
}
}
fn main() {
tools::run();
}Modules in Separate Files
Beyond inline modules, mod network; (with a semicolon, no body) tells Rust to load the contents from network.rs or network/mod.rs.
This is how real projects split code across files while keeping one module tree.
Privacy Boundaries
Modules form privacy boundaries. A child module can see its ancestors' private items, but parents cannot reach into a child's private items.
This encapsulation keeps internal details hidden.
mod bank {
fn secret() -> i32 { 42 }
pub fn reveal() -> i32 { secret() }
}
fn main() {
println!("{}", bank::reveal());
}Organizing Real Projects
Use modules to mirror your domain: auth, db, handlers. Keep each focused, expose a small public surface, and hide the rest.
Clear module boundaries make large codebases navigable.
Quick Check
Test your module knowledge.
Recap
You learned to organize code with modules:
mod name { ... }declares an inline module- Items are private by default; use
pubto expose them - Paths use
::to walk the module tree super,self, andmod name;navigate and split modules
الأسئلة الشائعة
هل درس «الوحدات وmod» مجاني؟
نعم — نص درس «الوحدات وmod» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Learn Rust Coding، انتقل إلى CoddyKit PRO. تتضمن دورة Learn Rust Coding 4 دروس في المجموع.
ماذا ستتعلم في «الوحدات وmod»؟
تنظيم الشيفرة تتمرن على Learn Rust Coding مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Learn Rust Coding؟
لا تُشترط خبرة سابقة. Learn Rust Coding على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «الوحدات وmod»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Learn Rust Coding هذا؟
نعم. كل درس في Learn Rust Coding يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.