Skip to content

Commit 5a85bfd

Browse files
authored
Update Rust档案系统.md
1 parent dd1ae72 commit 5a85bfd

File tree

1 file changed

+8
-149
lines changed

1 file changed

+8
-149
lines changed

入门秘笈/Rust档案系统.md

Lines changed: 8 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,8 @@
1-
# Rust模组
1+
# Rust档案系统
22

3-
模组是一个名称空间,包含函式或其型别的定义。模组是函式,结构,特征,预设情况下,模组的修饰符是私有的,但可以使用`impl``pub``public`
3+
------
44

5-
以下是模组中使用的关键字:
6-
7-
- **mod关键字**`mod`
8-
- **pub关键字 关键字使可见性修饰符成为公共,因此,它们可在名称空间外部存取。**`pub`
9-
- **use关键字**`use`
10-
11-
## 模组定义
12-
13-
模组由`mod`
14-
15-
模组的语法:
16-
17-
```rust
18-
mod module_name
19-
// body inside the module.
20-
```
21-
22-
模组可以通过三种方式分类:
23-
24-
**1.单个模组:**
25-
26-
通过下面一个例子来理解这一点:
27-
28-
```rust
29-
mod a
30-
{
31-
pub fn single_module()
32-
{
33-
println!("Single module");
34-
}
35-
}
36-
fn main()
37-
{
38-
a::single_module();
39-
}
40-
```
41-
42-
执行上面范例程式码,得到以下结果 -
43-
44-
```shell
45-
Single module
46-
```
47-
48-
在上面的范例中,定义了一个模组- 可以使用模组名称后跟名称空间然后使用函式名称来呼叫模组`a``a``a`
49-
50-
也可以使用单独的档案来执行上面的范例:
51-
52-
```rust
53-
mod module;
54-
fn main()
55-
{
56-
module::single_module();
57-
}
58-
pub fn single_module()
59-
{
60-
println!("Single module");
61-
}
62-
```
63-
64-
执行上面范例程式码,得到以下结果 -
65-
66-
```shell
67-
Single module
68-
```
69-
70-
在上面两个例子中,检查`mod X``X.rs``X/mod.rs`
71-
72-
**2.子模组:假设库名称是-**`language``C``Cplus`
73-
74-
下面给出了`language`
75-
76-
![img](https://tw511.com/upload/images/201910/20191014013926387.png)
77-
78-
通过下面一个例子来理解:
79-
80-
```rust
81-
mod c
82-
{
83-
pub fn c()
84-
{
85-
println!("C is a structured programming language");
86-
}
87-
}
88-
mod cplus
89-
{
90-
pub fn cplus()
91-
{
92-
println!("C++ is an object-oriented programming language");
93-
}
94-
}
95-
fn main()
96-
{
97-
c::c();
98-
cplus::cplus();
99-
}
100-
```
101-
102-
执行上面范例程式码,得到以下结果:
103-
104-
```shell
105-
C is a structured programming language
106-
C++ is an object-oriented programming language
107-
```
108-
109-
在上面的例子中,程式由两个模组组成,即`c``cplus``c::c()``cplus::cplus()`
110-
111-
**3.巢状模组:**
112-
113-
通过下面一个例子来理解这一点:
114-
115-
```rust
116-
mod a
117-
{
118-
pub fn a()
119-
{
120-
println!("a module");
121-
}
122-
pub mod b
123-
{
124-
pub fn a()
125-
{
126-
println!("b module");
127-
}
128-
}
129-
}
130-
fn main()
131-
{
132-
a::a();
133-
a::b::b();
134-
}
135-
```
136-
137-
执行上面范例程式码,得到以下结果 -
138-
139-
```shell
140-
a module
141-
b module
142-
```
143-
144-
在上面的例子中,程式由两个模组组成,即 这两个模组都包含具有相同名称但功能不同。这两个函式分别使用 它们都不会相互冲突,因为它们属于不同的名称空间。`a``b``b``a``a::a()``a::b::b()`
145-
146-
模组形成层次结构,使专案变得更容易理解。Rust模组系统用于分割多个档案,使得并非所有内容都位于`src/lib.rs``src/main.rs`
5+
模组形成层次结构,使专案变得更容易理解。Rust模组系统用于分割多个档案,使得并非所有内容都位于`src/lib.rs` `src/main.rs`
1476

1487
档案名:
1498

@@ -223,11 +82,11 @@ fn a()
22382
// block of statements.
22483
```
22584

226-
在这种情况下,不需要像在 并且,如果在这里编写`mod``mod``A`
85+
在这种情况下,不需要像在 并且,如果在这里编写`mod` `mod` `A`
22786

22887
Rust预设情况下会检视`src/lib.rs`
22988

230-
现在,从档案`src/lib.rs``B``B`
89+
现在,从档案`src/lib.rs` `B` `B`
23190

23291
档案名:
23392

@@ -254,7 +113,7 @@ mod B
254113
}
255114
```
256115

257-
现在建立包含模组 外部档案的名称将命名为- 建立档案后,在此档案中写入先前已删除的模组`B``src/B.rs``B`
116+
现在建立包含模组 外部档案的名称将命名为- 建立档案后,在此档案中写入先前已删除的模组`B` `src/B.rs` `B`
258117

259118
档案名:`src/B.rs`
260119

@@ -272,7 +131,7 @@ fn b()
272131
}
273132
```
274133

275-
现在将从档案`src/B.rs``C``C`
134+
现在将从档案`src/B.rs` `C` `C`
276135

277136
```rust
278137
fn b()
@@ -305,7 +164,7 @@ fn c()
305164
}
306165
```
307166

308-
> 注意:从模组B中提取模组C将导致编译错误,因为 因此,`src/B.rs``src/lib.rs``src/B.rs``src/B/mod.rs``B`
167+
> 注意:从模组B中提取模组C将导致编译错误,因为 因此,`src/B.rs` `src/lib.rs` `src/B.rs` `src/B/mod.rs` `B`
309168
310169
**模组档案系统规则:**
311170

0 commit comments

Comments
 (0)