The Rust Programming Language
- Module System
- Packages: A Cargo feature that lets you build, test, and share crates
- Crates: A tree of modules that produces a library or executable
- Modules and use: Let you control the organization, scope, and privacy of paths
- Paths: A way of naming an item, such as a struct, function, or module
- Packages and Crates
- Crate: the smallest amount of code that the Rust compiler considers at a time
- Crates can contain modules
- binary crate or library crate
- binary crate: must have a function called main
- library crate: don't have a main function, don't compile to an executable ex) rand crate
- Package: a bundle of one or more crates that provides a set of functionality
- Defining Modules to Control Scope and Privacy
- use keyword that brings a path into scope
- pub keyword make items public
- as keyword
- Modules Cheat Sheet
- Start from the crate root
- Declaring modules
- Declaring submodules
- Paths to code in modules
- private vs. public
- private is default
- pub mod
- The use keyword
- Grouping Related Code in Modules
- Paths for Referring to an Item in the Module Tree
- Path
- absolute path (starts with crate)
- relative path (self, super)
- Exposing Paths with the pub keyword
- making the module public doesn't make its contents public
- The pub keyword on a module only lets code in its ancestor modules refer to it
- Starting Relative Paths with super
- Making Structs and Enums Public
- enum public -> all of its variants are public
- Bringing Paths Into Scope with the use Keyword
- similar to creating a symbolic link in the file system
- Creating idomatic use Paths
- Specifying the parent module when calling the function makes it clear
that the function isn't locally defined while still minimizing repetition of the full path (ex. fn)
- when bringing in structs, enums, and other items -> idiomatic way is to specify the full path
- Providing New Names with the as keyword
- alias (add on the end of Path)
- Re-exporting Names with pub use
- Using External Packages
- Using Nested Paths to Clean Up Large use Lists
- can use self in the nested path
- The Glob Operator
- * glob operator
- often used when testing to bring everything under test into the tests module
- Separating Modules into Different Files
Managing Growing Projects with Packages, Crates, and Modules (영어)
'Personal-Study > Rust' 카테고리의 다른 글
[Rust 문서 읽기] 9. Error Handling (예외 처리) (0) | 2023.02.18 |
---|---|
[Rust 문서 읽기] 8. Common Collections (일반적인 컬렉션) (0) | 2023.02.11 |
[Rust 문서 읽기] 6. Enums and Pattern Matching (열거형과 패턴 매칭) (0) | 2023.02.05 |
[Rust 문서 읽기] 5. Using Structs to Structure Related Data (연관된 데이터들을 구조체로 다루기) (0) | 2023.02.05 |
[Rust 문서 읽기] 4. Understanding Ownership (소유권 이해하기) (0) | 2023.01.29 |
댓글