Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Paths

เข้าถึง items ใน module tree ด้วย paths

2 รูปแบบของ Path

Typeเริ่มจากSyntax
Absolutecrate rootcrate::module::item
Relativecurrent modulemodule::item

Absolute Path

เริ่มจาก crate root ด้วย crate:

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {
            println!("Adding to waitlist");
        }
    }
}

fn main() {
    // Absolute path - ชัดเจน ไม่กำกวม
    crate::front_of_house::hosting::add_to_waitlist();
}

Relative Path

เริ่มจาก module ปัจจุบัน:

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
    }
}

fn main() {
    // Relative path
    front_of_house::hosting::add_to_waitlist();
}

super Keyword

super = parent module (เหมือน .. ใน filesystem)

mod parent {
    fn parent_function() {
        println!("In parent");
    }

    pub mod child {
        pub fn call_parent() {
            // super ชี้ไปที่ parent module
            super::parent_function();
        }

        pub fn call_sibling() {
            // เรียก sibling module
            super::sibling::sibling_function();
        }
    }

    pub mod sibling {
        pub fn sibling_function() {
            println!("In sibling");
        }
    }
}

fn main() {
    parent::child::call_parent();
    parent::child::call_sibling();
}

self Keyword

self = current module

mod my_module {
    pub fn function_a() {
        println!("Function A");
    }

    pub fn function_b() {
        // ใช้ self เรียก function ใน module เดียวกัน
        self::function_a();

        // หรือเรียกตรงๆ ก็ได้
        function_a();
    }
}

ใช้ self กับ use

mod outer {
    pub mod inner {
        pub fn func() {}
    }

    pub fn use_inner() {
        // use self เพื่อนำเข้าจาก current module
        use self::inner::func;
        func();
    }
}

Path ใน use Statement

mod front_of_house {
    pub mod hosting {
        pub fn add_to_waitlist() {}
        pub fn seat_at_table() {}
    }
}

// นำเข้า module
use crate::front_of_house::hosting;

// หรือนำเข้า function โดยตรง
use crate::front_of_house::hosting::add_to_waitlist;

fn main() {
    // เรียกผ่าน module
    hosting::add_to_waitlist();

    // หรือเรียกตรง
    add_to_waitlist();
}

เลือกใช้ Absolute vs Relative?

Situationใช้
Code อาจย้ายAbsolute
อยู่ใกล้กันRelative
Re-exportAbsolute
Libraryแบบไหนก็ได้
// ✅ Good: ถ้าย้าย main ไปที่อื่น ไม่ต้องแก้
crate::utils::helper();

// ⚠️ Might break: ถ้าย้าย function นี้ไปที่อื่น
utils::helper();

ตัวอย่างจริง

mod database {
    pub mod connection {
        pub fn connect() {
            println!("Connecting...");
        }
    }

    pub mod queries {
        use super::connection;  // parent's sibling

        pub fn execute() {
            connection::connect();
            println!("Executing query...");
        }
    }
}

mod handlers {
    use crate::database;  // absolute path

    pub fn handle_request() {
        database::queries::execute();
    }
}

fn main() {
    handlers::handle_request();
}

ลองทำดู! 🎯

  1. สร้าง nested modules และใช้ super
  2. ใช้ self ใน use statement
  3. เปรียบเทียบ absolute และ relative paths

สรุป

Keywordความหมาย
crateRoot ของ crate ปัจจุบัน
superParent module
selfCurrent module
::Path separator

Path Examples

crate::module::item        // absolute
module::item               // relative
self::item                 // current module
super::item                // parent module
super::super::item         // grandparent

👉 ต่อไป: แยกไฟล์