From 9c794ecd372a02d22adb31ca6a067323a8940521 Mon Sep 17 00:00:00 2001 From: S John CD Date: Fri, 29 Dec 2023 18:50:04 +0000 Subject: [PATCH] Move modules examples to separate .rs files --- deps/examples/modules.rs | 33 +++++++++++++++++++++++++++++++++ deps/examples/modules2.rs | 15 +++++++++++++++ deps/examples/modules3.rs | 5 +++++ src/lang/modules.md | 36 +++--------------------------------- 4 files changed, 56 insertions(+), 33 deletions(-) create mode 100644 deps/examples/modules.rs create mode 100644 deps/examples/modules2.rs create mode 100644 deps/examples/modules3.rs diff --git a/deps/examples/modules.rs b/deps/examples/modules.rs new file mode 100644 index 00000000..1edc0d06 --- /dev/null +++ b/deps/examples/modules.rs @@ -0,0 +1,33 @@ +#![allow(unused_imports, dead_code)] + +use std::collections::HashMap; +use std::fs::File; // `File` without prefix is now available in the scope // For code from an external crate, the absolute path begins with the crate name - here, the standard `std` library + +use std::collections::*; // Glob - all public objects + +use std::io::{self, Write}; +use std::{cmp::Ordering, fmt}; // Combining multiple use lines together + +mod utils { + pub fn insert_use() {} +} +use crate::utils::insert_use; // Absolute path - for code from the current crate, it starts with the literal crate. + +mod a { + pub mod b {} +} +use self::a::b; // A relative path starts from the current module and uses self, super, or an identifier in the current module. + +mod c { + use super::a; // We can construct relative paths that begin in the parent module, rather than the current module or the crate root, by using super at the start of the path. +} + +use std::fmt::Result; +use std::io::Result as IoResult; // Alias + +mod front_of_house { + pub mod hosting {} +} +pub use crate::front_of_house::hosting; // Reexporting - Now that this pub use has re-exported the hosting module from the root module, external code can now use the path ::hosting::add_to_waitlist() instead. + +fn main() {} diff --git a/deps/examples/modules2.rs b/deps/examples/modules2.rs new file mode 100644 index 00000000..d418204b --- /dev/null +++ b/deps/examples/modules2.rs @@ -0,0 +1,15 @@ +use front_of_house::hosting; + +mod front_of_house { + pub mod hosting { + pub fn add_to_waitlist() {} + } +} + +fn eat_at_restaurant() { + hosting::add_to_waitlist(); +} + +fn main() { + eat_at_restaurant(); +} diff --git a/deps/examples/modules3.rs b/deps/examples/modules3.rs new file mode 100644 index 00000000..4bdbf0cf --- /dev/null +++ b/deps/examples/modules3.rs @@ -0,0 +1,5 @@ +use std::collections::HashMap; + +fn main() { + let mut _map: HashMap = HashMap::new(); +} diff --git a/src/lang/modules.md b/src/lang/modules.md index 9cd55f64..ffe5f282 100644 --- a/src/lang/modules.md +++ b/src/lang/modules.md @@ -30,47 +30,17 @@ Create a shortcut to a path with the `use` keyword once, and then use the shorte [Use]( https://doc.rust-lang.org/rust-by-example/mod/use.html ) ```rust,editable,ignore -use std::fs::File; // `File` without prefix is now available in the scope -use std::collections::HashMap; // for code from an external crate, the absolute path begins with the crate name - here, the standard `std` library - -use std::collections::*; // Glob - all public objects - -use itertools::Itertools; -use syntax::ast; - -use std::{cmp::Ordering, io}; // combining multiple use lines together -use std::io::{self, Write}; - -use crate::utils::insert_use; // absolute path - for code from the current crate, it starts with the literal crate. - -use self::auto_import; // A relative path starts from the current module and uses self, super, or an identifier in the current module. - -use super::AssistContext; // We can construct relative paths that begin in the parent module, rather than the current module or the crate root, by using super at the start of the path. - -use std::fmt::Result; -use std::io::Result as IoResult; // Alias - -pub use crate::front_of_house::hosting; // Reexporting - Now that this pub use has re-exported the hosting module from the root module, external code can now use the path ::hosting::add_to_waitlist() instead. - -fn main() {} +{{#include ../../deps/examples/modules.rs}} ``` Idiomatic - bringing the function’s parent module into scope, not the function itself: ```rust,editable,ignore -use crate::front_of_house::hosting; - -pub fn eat_at_restaurant() { - hosting::add_to_waitlist(); -} +{{#include ../../deps/examples/modules2.rs}} ``` On the other hand, when bringing in structs, enums, and other items with use, it’s idiomatic to specify the full path. ```rust,editable -use std::collections::HashMap; - -fn main() { - let mut map: HashMap = HashMap::new(); -} +{{#include ../../deps/examples/modules3.rs}} ```