[易学易懂系列|rustlang语言|零基础|快速入门|(16)|代码组织与模块化]
[易学易懂系列|rustlang语言|零基础|快速入门|(16)|代码组织与模块化]
实用知识
代码组织与模块化
我们知道,在现代软件开发的过程中,代码组织和模块化是应对复杂性的一种方式。
今天我们来看看Rust是怎么做代码组织和模块化的。
Rust用mod 关键字来定义模块。
我们还是拿上一篇文章中的代码来作例子,我们在原来的代码lib.rs加入新的mod:
mod greetings {
// ⭐️ By default, everything inside a module is private
pub fn hello() -> String {
// ⭐️ So function has to be public to access from outside
return String::from("Hello, world!");
}
}
其中:pub fn hello(),是定义一个公共的方法hello。
Rust用关键词pub关键词来定义公共的方法,代表这个方法可以在这个模块外访问。
一般情况下,fn默认是私有的,也就是说它只能在本模块或本模块的子模块内访问。
这个跟一般的开发语言,比如java的访问控制习惯是一样的。好理解。
我们来看看完整的代码:
fn greet() -> String {
// return getString();
return greetings::hello();
}
mod greetings {
// ⭐️ By default, everything inside a module is private
pub fn hello() -> String {
// ⭐️ So function has to be public to access from outside
return String::from("Hello, world!");
}
//private funtions
fn getString() -> String {
return String::from("Hello, world!");
}
}
#[cfg(test)] // Only compiles when running tests
mod tests;
我们用cargo test运行测试代码,得到的结果是pass的。
模块也是可以嵌套的,我们在lib.rs加入如下代码 :
mod phrases {
pub mod greetings {
pub fn hello() -> String {
return String::from("Hello, world!");
}
}
}
lib.rs完整代码如下 :
fn greet() -> String {
// return getString();
//return greetings::hello();
return phrases::greetings::hello();
}
mod greetings {
// ⭐️ By default, everything inside a module is private
pub fn hello() -> String {
// ⭐️ So function has to be public to access from outside
return String::from("Hello, world!");
}
//private funtions
fn getString() -> String {
return String::from("Hello, world!");
}
}
mod phrases {
pub mod greetings {
pub fn hello() -> String {
return String::from("Hello, world!");
}
}
}
#[cfg(test)] // Only compiles when running tests
mod tests;
同样我们用命令运行:cargo test
得到的结果是:
running 2 tests
test tests::it_works ... ok
test tests::test_greet ... ok
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
我们再把方法移到另一个文件greetter.rs中,如下:
// ↳ greetter.rs
// ⭐️ No need to wrap the code with a mod declaration. The file itself acts as a module.}
pub fn hello() -> String {
// The function has to be public to access from outside{
return String::from("Hello, world!");
}
lib.rs中的代码如下 :
mod greetter; // Import greetter module
fn greet() -> String {
return greetter::hello();
}
#[cfg(test)] // Only compiles when running tests
mod tests;
我们再运行测试,结果应该是全通过。
现在我们再来看看不同文件中的嵌套模块的代码,我们新建一个文件phrasing.rs:
// ↳ phrasing.rs
pub mod greetings {
// ⭐️ The module has to be public to access from outside
pub fn hello() -> String {
// The function has to be public to access from outside{
return String::from("Hello, world!");
}
}
然后我们在lib.rs中文件中调用相关方法,如下 :
// mod greetter; // Import greetings module
mod phrasing;
fn greet() -> String {
return phrasing::greetings::hello();
}
#[cfg(test)] // Only compiles when running tests
mod tests;
以上代码,也测试通过。
现在我们来看看,另一种情况:不同文件,不同目录。
我们在src目录中新建一个目录:calling。
在目录calling中,我们新建一个mob.rs,我们在这个mob.rs中写入如下代码:
pub fn hello() -> String {
// ⭐️ The function has to be public to access from outside
return String::from("Hello, world!");
}
在Rust中,mob.rs指向当前根目录,代表当前目录的模块,这里是:calling目录。
我们在怎么引用呢?
看我们在lib.rs的代码:
mod calling;// Import calling module
fn greet() -> String {
return calling::hello();
}
#[cfg(test)] // Only compiles when running tests
mod tests;
同样,我们测试是通过的。
我们再看看在mod.rs文件定义一个mod,这也代表一个嵌套mod模块:
//calling/mod.rs
pub mod greetings {
// ⭐️ The module has to be public to access from outside
pub fn hello() -> String {
return String::from("Hello, world!");
}
}
我们在lib.rs中调用,则用如下代码:
mod calling;// Import calling module
fn greet() -> String {
return calling::greetings::hello();
}
#[cfg(test)] // Only compiles when running tests
mod tests;
我们再来看看另一种情况,我们在calling目录下新建一个文件other.rs,代码:
//calling/other.rs
pub fn hello() -> String {
// The function has to be public to access from outside{
return String::from("Hello, world!");
}
这时我们在calling/mob.rs文件中的代码如下:
mod other;// Import other module
pub fn hello() -> String {
// ⭐️ The function has to be public to access from outside
return other::hello();
}
我们在lib.rs的代码如下 :
// mod greetter; // Import greetings module
mod calling; // Import calling module
fn greet() -> String {
return calling::hello();
}
#[cfg(test)] // Only compiles when running tests
mod tests;
我们发现这种方式好像复杂了一点,能否直接从lib.rs直接调用other.rs的代码呢?
可以的。
我们只要把文件:calling/mob.rs的代码改为如下 代码 :
pub mod other;
这段代码的含义是:把other.rs这个模块导入为公共模块。
这时,我们的lib.rs代码如下 :
mod calling; // Import calling module
fn greet() -> String {
return calling::other::hello();//直接可以访问other模块的方法hello()
}
#[cfg(test)] // Only compiles when running tests
mod tests;
以上,希望对你有用。
如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust
参考:https://learning-rust.github.io/docs/d3.modules.html
[易学易懂系列|rustlang语言|零基础|快速入门|(16)|代码组织与模块化]的更多相关文章
- [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]
[易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具] 项目实战 实战5:实现BTC价格转换工具 今天我们来开发一个简单的BTC实时价格转换工具. 我们首先 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链]
[易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链] 项目实战 实战4:从零实现BTC区块链 我们今天来开发我们的BTC区块链系统. 简单来说,从数据结构的 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)]
[易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)] 项目实战 实战3:Http服务器 我们今天来进一步开发我们的Http服务器,用多线程实现. 我 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)]
[易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)] 项目实战 实战2:命令行工具minigrep 我们继续开发我们的minigrep. 我们现 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)]
[易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)] 项目实战 实战2:命令行工具minigrep 有了昨天的基础,我们今天来开始另一个稍微有点 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]
[易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏] 项目实战 实战1:猜数字游戏 我们今天来来开始简单的项目实战. 第一个简单项目是猜数字游戏. 简单来说,系统给了 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(5)|生命周期Lifetime]
[易学易懂系列|rustlang语言|零基础|快速入门|(5)] Lifetimes 我们继续谈谈生命周期(lifttime),我们还是拿代码来说话: fn main() { let mut a = ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro]
[易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针]
[易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针] 实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值 ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理]
[易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理] 实用知识 错误处理 我们今天来讲讲Rust中的错误处理. 很多语言都有自己的错误处理方式,比如,java是异常处理机制. ...
随机推荐
- 掌握Pod-Pod调度策略
一 Pod生命周期管理 1.1 Pod生命周期 Pod在整个生命周期过程中被系统定义了如下各种状态. 状态值 描述 Pending API Server已经创建该Pod,且Pod内还有一个或多个容器的 ...
- C#字符串内插-$
1.字符串内插 $特殊字符将字符串文本标识未内插字符串,可能包含内插表达式的字符串文本. 将内插字符串解析为结果字符串,带有内插表达式的项会替换为表达式结果的字符串表示形式. 在C#6级更高版本语言中 ...
- Jquery(DOM和选择器)
O(∩_∩)O~~~,今天简单整理了一下最近所学的Jquery知识.下面就总结一下. 首先,对于Jquery我们需要简单了解下: 1.Jquery是开放源代码的JS库, 2.Jquery操作是函数式编 ...
- mybatis-generator自动生成代码时,只生成insert方法
今天使用mybatis-generator自动生成代码时,发现只能生成insert方法, 以前所有的方法都是可以生成的,查看网上解决办法和检查数据库表结构后, 发现2种可以解决的办法: 1.修改myb ...
- 已经配置好了的 jmeter + ant 框架
已经配置好了的 jmeter + ant 框架 ,需要自取,避免查找安装攻略时耗费时间 使用前需配置环境变量,阅读文件内安装文档!!! 链接:https://pan.baidu.com/s/1eRz9 ...
- (转)当margin-top、padding-top的值为百分比时是如何计算的?
本文链接:https://blog.csdn.net/qq_27437967/article/details/72625900问题:当margin-top.padding-top的值是百分比时,分别是 ...
- 5表联查yii框架权限控制
一:控制器部分 <?php namespace app\controllers; use yii\web\Controller; class PreController extends Cont ...
- 从入门到自闭之Python函数初识
函数初识 定义:def--关键字 将某个功能封装到一个空间中就是一个函数 功能: 减少重复代码 函数的调用 函数名+():调用函数和接收返回值 函数的返回值 return 值 == 返回值 ...
- python_0基础开始_day11
第十一节 一,函数名的第一类对象 函数名当作值,赋值给变量 print(函数名) 查看看书的内存地址 函数名可以当作容器中的元素 lis = []dic = {}def func(): prin ...
- Restful Api调用工具类
RestfulHttpClient.java package pres.lnk.utils; import com.fasterxml.jackson.databind.ObjectMapper; i ...