In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use the Rust module. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Preface
This article is intended for beginners of rust to help understand how rust modules reference each other. This article tries to demonstrate with very little code, so you can get familiar with it even if you haven't known rust before. You can make an impression when you use it.
If you haven't known rust before, you just need to know: Cargo- relies on management tools, similar to npm,Cargo 's use of the file placement convention, that is, the file name is the module name. Crate- container is a set of functional packages, similar to npm packages.
The scenario discussed in this paper is that when the code is split and modules are extracted to varying degrees in the project, it is often necessary to introduce part of the code of another module into one file. In javascript, we can import and export the code that uses other modules. In this process, we only need to care about whether the import path is correct.
Export const name= xx import lodash from'. / lodash'
In rust, the module is no longer introduced through the file path, but through cargo and the agreed module declaration to build the module tree, and then used by the use keyword. However, the examples of file splitting and module use in rust's documentation are not very detailed, so it is combed with reference to the organization of some released crate.
Module declaration & use the
Suppose we want to implement an addition module and provide it for use elsewhere. We can organize in the following three ways
Cargo uses the file placement convention, so module search is based on rs files or directories in the src directory, and will only find one level. Rs files in nested folders cannot be used directly by other files.
Method 1: declare add.rs directly under the root file
Cargo can recognize the add module by adding a file with the same name under src.
├── Cargo.lock ├── Cargo.toml ├── src │ ├── add.rs │ ├── lib.rs method 2: declare the add folder, which contains mod.rs
If the module is a folder, you must have an mod.rs file. This is similar to javascript's index.js. Cargo can still recognize that this is the add module.
├── Cargo.lock ├── Cargo.toml ├── src │ ├── add │ │ ├── mod.rs │ ├── lib.rs
Suppose our code is as follows and is located in the file add.rs or add/mod.rs
Pub fn add_fn (a: i32, b: i32)-> i32 {a + b}
So in lib.rs, we can call our add module as follows
/ / declare the module and refer to the function mod add;pub use crate::add::add_fn;pub fn test_lib () {add_fn (1Magazine 2) in the module;} method 3: add.rs and add folders exist at the same time
The directory structure in this way looks like this
├── Cargo.lock ├── Cargo.toml ├── src │ ├── add │ │ └── add_m.rs │ ├── add.rs / / index.js │ ├── lib.rs
Add.rs is responsible for the import and export of the entry module, and the rest of the associated modules are stored in the add folder. This is similar to javascript's index.js, which uniformly exports multiple other modules. Unlike the above, the import here uses the mod keyword to split the module.
The contents of the file look like this
Add.rs
Pub mod add_m;// is similar to export * from'. / validate; export * from'. / helper'
Add/add_m.rs
Pub fn add_fn (a: i32, b: i32)-> i32 {a + b}
Lib.rs
Mod add;pub use crate::add::add_m::add_fn;pub fn test_lib () {add_fn (1pm 2);}
The above three methods should be more used in the first two, and the second is more reasonable and can better organize documents in large-scale projects. So how to call adjacent files when splitting multiple module files under a module folder?
Reference of adjacent files in the same module
We adjust the directory structure as follows
├── Cargo.lock ├── Cargo.toml ├── src │ ├── add │ │ ├── mod.rs │ │ ├── print.rs │ │ └── user.rs / / user calls print's method │ ├── lib.rs
Print and user are added under the add module. User calls the method of print.
Print.rs
Pub mod my_print {pub fn print_hello () {println! (hello)}} / / the pub mod here can be simply understood as the declare module of ts, which contains the available attributes of module / / declare module my_print {/ / export function print_hello (): string;//}
User.rs
Use super::print::my_print;pub fn hello_user () {my_print::print_hello ();} pub struct User {pub name: String,}
Use the super keyword for referencing files under the same module.
Mod.rs
/ / mod.rs is the entry file. The following declaration with mod will first look for files with the same name under the same folder. If not, see if there is a folder with the same name under the condition / / for example, if there is no print.rs under the add folder, find out if there is a print folder and there is mod.rs under the folder. Mod print;mod user;// should be selfpub use self::user::hello_user;pub use self::user::User;pub mod add_fn {/ / use super::* because it is under the same module folder and is used in the entry file. If you have this line, you don't have to call super pub fn add (a: i32, b: i32)-> i32 {/ / notice the super keyword here, because hello_user is declared in another module and cannot be called directly between modules, so you need to use super to find and call super::hello_user () from the module root. Let value = super::User {name: String::from (Rust),}; println! (user name {}, value.name); a + b}} pub fn test_out_ref () {/ / super is not needed here because hello_user () is not defined in the mod;} different module references
We add a new module multip, which returns the result of multiplying two numbers. The directory structure is as follows
├── Cargo.lock ├── Cargo.toml ├── src │ ├── add │ │ ├── mod.rs │ │ print.rs │ │ └── user.rs / / user will call print's method │ ├── multip / /-add this module │ │ ├── mod.rs method lib.rs
Multip/mod.rs
Pub fn res_multip (a: i32, b: i32)-> i32 {a * b}
Suppose the add file introduces multip
Mod print;mod user;pub use self::user::hello_user;pub use self::user::User;// added the following line of use crate::multi::multip
This allows you to use the contents of another module. Of course, other modules refer to each other in the same way.
This is the end of the article on "how to use the Rust module". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.