- Java锁的逻辑(结合对象头和ObjectMonitor)
- 还在用饼状图?来瞧瞧这些炫酷的百分比可视化新图形(附代码实现)⛵
- 自动注册实体类到EntityFrameworkCore上下文,并适配ABP及ABPVNext
- 基于Sklearn机器学习代码实战
这篇文章是实战性质的,也就是说原理部分较少,属于经验总结,rust对于模块的例子太少了。rust特性比较多(悲), 本文的内容可能只是一部分,实现方式也不一定是这一种 .
关于 rust 模块的相关内容,准确来说: 怎么在源码中引用其他模块的内容 .
mod
、 use
、 as
这几个关键字(文件名) mod.rs
文件 self
、 super
、 crate
这几个路径关键字 worksapce
:本文不讨论,狭义上指的是cargo的 [workspace]
部分: 可参见 The [workspace] section package
: 狭义上指的是cargo的 [package]
部分,参见 The [package] section crate
:参见下文 关于 create 的定义和 cargo 管理下的 crate module
:本文的重点, crate 里 会有多个 module,文本讨论重点就是 mod 之间相互引用的问题。 引用模块要搞清楚的:
mod 关键字:
mod xxx { <rust语句块> }
:内部写 rust 语句 。见 例一 #[path="...xxxx.rs"] mod xxx;
:使用 path 属性 ,使用见例二 mod xxx { include!("...xxxx.rs") }
:内部配合 include!
宏,使用见例二 mod toy;
语句来引入一个模块,实际上这跟你在哪里写的这个语句有关系有关
src/main.rs
、 src/lib.rs
或者 xxx/.../mod.rs
位置上的文件; 类型B: 位置不在1中的文件。 mod toy;
,你实际上在告诉编译器,你需要的模块是与此文件同级的 toy.rs
或者 toy/mod.rs
文件 。编译器会自己找下这两个被引用的位置,如果两个位置都有文件,则报错。见 例三 mod toy;
,你实际上在告诉编译器,你需要的模块是与此文件(假设文件名为 foo.rs
)同级的 foo
文件夹下的 foo/toy.rs
或者 foo/toy/mod.rs
文件。编译器会自己找下这两个被引用的位置,如果两个位置都有文件,则报错。 见 例四 use 关键字也有两个作用 。
as
关键字一起使用,进一步减少重复代码,或者防止名字重复,或者取个顺眼的名字 例子六 如果我们想要调用一个函数,我们需要知道它的路径。路径有两种形式:
见文档: https://rustwiki.org/zh-CN/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html 。
我们都知道通过 cargo 创建出的工程中 src/main.rs 就是程序的入口,但是还有更多的使用方式.
下面就是一些问题了 。
bin
和 lib
,其他类型参见 rust参考手册-链接
▾ src/ # 包含源文件的目录
lib.rs # 库和包的主要入口点
main.rs # 包生成可执行文件的主要入口点
▾ bin/ # (可选)包含其他可执行文件的目录
*.rs
▾ */ # (可选)包含多文件可执行文件的目录
main.rs
▾ examples/ # (可选)示例
*.rs
▾ */ # (可选)包含多文件示例的目录
main.rs
▾ tests/ # (可选)集成测试
*.rs
▾ */ # (可选)包含多文件测试的目录
main.rs
▾ benches/ # (可选)基准
*.rs
▾ */ # (可选)包含多文件基准的目录
main.rs
toy
模块 run
函数需要加 pub
关键字,否则不会被导出 src/main.rs 。
mod toy {
pub fn run() {
println!("run toy");
}
}
fn main() {
toy::run();
}
输出 。
run toy
toy
模块 src/toy_implements.rs 。
pub fn run() {
println!("run toy_impl !");
}
src/main.rs 。
mod toy1 { // 方法1: 使用 include!
include!("./toy_implements.rs");
}
#[path ="./toy_implements.rs"]
mod toy2; // 方法2: 使用 path 属性定位文件位置
fn main() {
toy1::run();
toy2::run();
}
输出 。
run toy_impl !
run toy_impl !
src/toy.rs 。
pub fn run() {
println!("run toy_impl !");
}
src/main.rs 。
mod toy;
fn main() {
toy::run();
}
输出 。
run toy_impl !
src/foo/toy.rs 。
pub fn run() {
println!("run toy_impl !");
}
src/foo.rs 。
mod toy;
fn say_hi() {
toy::run();
}
输出 。
run toy_impl !
之前,我们使用了 toy::run() 来调用 run 函数。现在,我们使用 use 关键字来导入 toy 模块里的内容,这样就能在 main 函数中直接使用 。
src/foo.rs 。
mod toy {
pub fn run() { // 注意使用 pub 关键字
println!("run toy");
}
}
fn main() {
use toy::*; // 使用 use 导入 toy 模块里的内容
run(); // 直接调用
}
src/foo.rs 。
mod toy {
pub fn run() { // 注意使用 pub 关键字
println!("run toy");
}
}
fn main() {
use toy::run as toy_run; // 使用 use + as 导入 toy 模块里的内容
toy_run();
}
src/toy/runner.rs 。
pub fn dog_run() { println!("dog is run !"); }
src/toy/fly.rs 。
pub fn fly_bird() { println!("bird is fly !"); }
src/toy/bear.rs 。
pub fn bear_eat() { println!("bear is eat fish !"); }
pub fn bear_sleep() { println!("bear is go sleep !"); }
src/toy/mod.rs 。
mod runner; // 引入同级 runner.rs 文件
mod fly; // 引入同级 fly.rs 文件
mod bear; // 引入同级 bear.rs 文件
pub use runner::dog_run; // 声明(导出) dog_run 函数
pub use fly::fly_bird as now_fly_brid; // 声明(导出) fly_bird 函数,并重命名为 now_fly_brid
pub use bear::*; // 声明(导出) dog_run 函数
src/main.rs 。
mod toy;
fn main() {
toy::dog_run();
toy::now_fly_brid();
toy::bear_eat();
toy::bear_sleep();
}
输出 。
dog is run !
bird is fly !
bear is eat fish !
bear is go sleep !
src/toy/cube/mod.rs 。
pub fn get_size() {
println!("size is in main");
crate::top_size(); // 必不可少的 crate 关键字
}
src/toy/mod.rs 。
pub mod cube;
src/main.rs 。
mod toy;
fn top_size() {
println!("top size one !")
}
fn main() {
toy::cube::get_size();
}
输出 。
size is in main
top size one !
!!强烈推荐看下面的参考做补充!! 。
最后此篇关于【rust】rsut基础:模块的使用一、mod关键字、mod.rs文件的含义等的文章就讲到这里了,如果你想了解更多关于【rust】rsut基础:模块的使用一、mod关键字、mod.rs文件的含义等的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
来自 JSR-339: For simplicity, JAX-RS implementations are NOT REQUIRED to support processing groups oth
JAX-RS 提供了 StreamingOutput 接口(interface),我们可以实现它来对我们的响应主体进行原始流处理。 public interface StreamingOutput {
有没有办法获得java.lang.reflect.Method为给定的 @Path 调用的方法(用 HttpServletRequest 注释) ? 这是我的用例:我在 Java EE 中 Filte
我接到了一家公司的任务,该任务向我发送了一台已完成所有设置的虚拟机。任务是我必须创建一个 API 来从数据库中检索人员详细信息并显示它。 问题是,当我运行应用程序时,服务器返回一个包含 hello w
我有一个 POST 方法调用,它接受很少的表单参数。我希望 JAX-RS 能够处理不存在特定表单参数的情况。 示例: @POST @Produces (MediaType.APPLICATION_JS
我有 JAX-RS 网络应用程序,我想记录从获取请求到响应的时间量。在带有 servlet 过滤器的 Spring Boot 中很容易。但是我的应用程序中的过滤器无法正常工作: @Provider p
使用以下网址。 http://doma.in/context/resource/some/.../undefined 我想获取 ../resource 之后的路径名,即 /some/.../undef
Closed. This question is off-topic。它当前不接受答案。
我花了几个小时在嵌入式 Jetty 9.1.0.v20131115 和 RESTEasy 3.0.5.Final 中安装自定义登录服务。我的登录服务将在数据库中查找用户并为他们分配角色。它看起来像这样
RESOURCE_STALLS.RS的说明Intel Broadwell 的硬件性能事件如下: This event counts stall cycles caused by absence of
我想从 java 服务器 (Restful Jax-rs) 发送图像。我的客户是安卓。 @GET public Response getUserImage() { byte[] image =new
(特别是 RESTeasy) 具有如下方法签名会很好(对于单个文件): public void upload(@FormParam("name") ..., @FormParam("file") fi
RESTEasy (JAX-RS) 允许通过子资源进行动态调度。例如: POST /customers/create {"name":"Smith","country":"jp"} 我们可以有一个根资
假设有一个 ResultSet rs 和 n 对象。 这段代码: while(rs.next()) { // do something on rs } 在算法上等于此代码(即两者给出相同的结果)
我正在使用 Visual Studio 2005 和 mysql 4.0,我有一个表,其中包含一个字段(十进制 16,4),用于存储发票金额和另一个表,其中包含对该发票的付款(这些字段也是十进制的)
我正在使用 JAX-RS、Microprofile 和 Payara 5 构建 REST 服务。我的方法返回类型为 Response 的对象。响应本身包含一个 MyClass 列表。实现看起来像这样:
我已经使用 Mock MVC 成功地为 Spring MVC REST 服务构建了无需部署的集成测试框架。我正在努力对 JAX-RS 服务做同样的事情。 REST Assured 看起来很有希望,但它
我有一个使用JAX-RS 2.0的Jersey实现的Java应用程序,并且我想在客户端启用gzip压缩。服务器已启用它,并且通过在Chrome中的开发人员工具中的“大小/内容”中查找客户端正在使用的特
我正在尝试与 Tomcat7 一起使用 RESTful Web 服务 (JAX-RS)。我尝试了 3 种不同的实现(Jersey、RESTeasy 和 ReSTLet),但都没有成功。这应该很容易,但
swagger-api有什么区别和 JAX-RS ? 是swagger-api仅用于文档? (例如 @ApiOperation ) 最佳答案 根据 API 文档,JAX-RS是用于 RESTful W
我是一名优秀的程序员,十分优秀!