Rust_从找门到入门 | 项目组织结构

从接触Rust到现在一年多了,目前还是没有找到门,看之前写的自学笔记,可读性几乎为零。现在这篇尽量用“人话”记录一下我对Rust项目组织结构的理解。

首先是 package 和 crate 之间的关系。

Package 里可以有一个或者多个 crate(但是不能没有),这些crate 中最多有一个 library crate(也可以没有),binary crate个数不限(如果没有 library crate,可想而知必须至少得有个binary crate;如果有 library crate,可能没有 binary crate,有点绕……)。

A crate will group related functionality together in a scope so the functionality is easy to share between multiple projects.

Crate 主要集成功能。

Modules let us organize code within a crate into groups for readability and easy reuse. Modules also control the privacy of items, which is whether an item can be used by outside code (public) or is an internal implementation detail and not available for outside use (private).

Module 增加代码可读性、复用性,以及隐私性。

绝对路径和相对路径均可, 绝对路径更具普遍性,具体情况具体分析。

使用useas,以及mod mod_name;来拆分module为多文件。

有一个实操网站

Rust automagically looks for it inside the file, if doesn’t find it, looks for a file with the module name in the same folder (in this case src/) and if still doesn’t find it looks for a folder with the module name and a file mod.rs inside, there it looks for the code.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值