[易学易懂系列|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)|代码组织与模块化]的更多相关文章

  1. [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具]

    [易学易懂系列|rustlang语言|零基础|快速入门|(28)|实战5:实现BTC价格转换工具] 项目实战 实战5:实现BTC价格转换工具 今天我们来开发一个简单的BTC实时价格转换工具. 我们首先 ...

  2. [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链]

    [易学易懂系列|rustlang语言|零基础|快速入门|(27)|实战4:从零实现BTC区块链] 项目实战 实战4:从零实现BTC区块链 我们今天来开发我们的BTC区块链系统. 简单来说,从数据结构的 ...

  3. [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(26)|实战3:Http服务器(多线程版本)] 项目实战 实战3:Http服务器 我们今天来进一步开发我们的Http服务器,用多线程实现. 我 ...

  4. [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(25)|实战2:命令行工具minigrep(2)] 项目实战 实战2:命令行工具minigrep 我们继续开发我们的minigrep. 我们现 ...

  5. [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)]

    [易学易懂系列|rustlang语言|零基础|快速入门|(24)|实战2:命令行工具minigrep(1)] 项目实战 实战2:命令行工具minigrep 有了昨天的基础,我们今天来开始另一个稍微有点 ...

  6. [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏]

    [易学易懂系列|rustlang语言|零基础|快速入门|(23)|实战1:猜数字游戏] 项目实战 实战1:猜数字游戏 我们今天来来开始简单的项目实战. 第一个简单项目是猜数字游戏. 简单来说,系统给了 ...

  7. [易学易懂系列|rustlang语言|零基础|快速入门|(5)|生命周期Lifetime]

    [易学易懂系列|rustlang语言|零基础|快速入门|(5)] Lifetimes 我们继续谈谈生命周期(lifttime),我们还是拿代码来说话: fn main() { let mut a = ...

  8. [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro]

    [易学易懂系列|rustlang语言|零基础|快速入门|(22)|宏Macro] 实用知识 宏Macro 我们今天来讲讲Rust中强大的宏Macro. Rust的宏macro是实现元编程的强大工具. ...

  9. [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针]

    [易学易懂系列|rustlang语言|零基础|快速入门|(21)|智能指针] 实用知识 智能指针 我们今天来讲讲Rust中的智能指针. 什么是指针? 在Rust,指针(普通指针),就是保存内存地址的值 ...

  10. [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理]

    [易学易懂系列|rustlang语言|零基础|快速入门|(20)|错误处理] 实用知识 错误处理 我们今天来讲讲Rust中的错误处理. 很多语言都有自己的错误处理方式,比如,java是异常处理机制. ...

随机推荐

  1. if、elif 条件判断

    #!/usr/bin/python #-*- codinig: UTF-8 -*- from __future__ import print_function import os, sys, stat ...

  2. JavaScript DOM 编程艺术(第二版) 初读学习笔记

    这本书留给我的印象就是结构.表现和行为层的分离,以及书后面部分一直在强调的最佳实践原则:平稳退化,逐步增强,向后兼容以及性能考虑. 要注意这不是一本JavaScript入门书籍~ 2.1 准备工作 用 ...

  3. 一步一步搭建:spark之Standalone模式+zookeeper之HA机制

    理论参考:http://www.cnblogs.com/hseagle/p/3673147.html 基于3台主机搭建:以下仅是操作步骤,原理网上自查 :1. 增加ip和hostname的对应关系,跨 ...

  4. cryptopp 加密库的安装

    今天 在搭建环境的过程中遇到一个问题:C++ 的加密库 crypto在新系统中没有安装,于是百度一下,顺便解决问题 1.开源包下载 下载地址:https://www.cryptopp.com/#dow ...

  5. XCTF (app2)

    打开app,有两个输入框和一个按钮.点击按钮会跳转到新的页面显示Waiting for you. 打开JEB反编译. 如果两个输入框的长度都不为0,那么获取这两个值到v0和v1中,Log记录日志. 创 ...

  6. 如何搭建本地yum源,阿里yum源以及自己的网络yum源?

    环境:CentOS7 一.本地源的yum源的搭建 (一)添加新的yum源配置文件iso.repo(名字可以自己命名,但是后缀必须是repo结尾) 注意:目录 /etc/yum.repos.d 下的 . ...

  7. 【Python开发】python发送各类邮件的方法

    转载: python发送各类邮件的主要方法 python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点. 一.相关模块介绍 发送 ...

  8. Identification of Encryption Algorithm Using Decision Tree

    本文主要做了两件事,一是提出了一种使用C4.5算法生成的决策树来识别密文所使用的加密算法的方法,二是为这一算法设计了一个特征提取系统提取八个特征作为算法的输入,最终实现了70%~75的准确率. 准备工 ...

  9. Python globals()和locals()比较

    Python的两个内置函数,globals()和locals() ,它们提供了基于字典的访问局部和全局变量的方式. globals()是可写的,即,可修改该字典中的键值,可新增和删除键值对. 而loc ...

  10. jump用户管理命令

    ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa手动拷贝id_rsa.pub的内容到其他机器 或者用 ssh-copy,但你得知道对方root密码 ssh-copy-i ...