mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-02-03 07:48:41 +08:00
Label all code with a filename
This commit is contained in:
parent
9e81b8c9b5
commit
37d5d555f8
@ -15,8 +15,10 @@ $ cargo new communicator
|
||||
$ cd communicator
|
||||
```
|
||||
|
||||
You'll notice that Cargo generated `src/lib.rs` instead of `src/main.rs` for
|
||||
us, and inside it we'll find this:
|
||||
Notice that Cargo generated `src/lib.rs` instead of `src/main.rs` for us, and
|
||||
inside it we'll find this:
|
||||
|
||||
Filename: src/lib.rs
|
||||
|
||||
```rust
|
||||
#[cfg(test)]
|
||||
@ -55,6 +57,8 @@ rather than `connect()`.
|
||||
You could have multiple modules, side-by-side. For example, if you wanted a
|
||||
`client` module:
|
||||
|
||||
Filename: src/lib.rs
|
||||
|
||||
```rust
|
||||
mod network {
|
||||
fn connect() {
|
||||
@ -72,6 +76,8 @@ Now we have a `network::connect` function and a `client::connect` function.
|
||||
And you can put modules inside of modules. If you wanted to have `client` be
|
||||
within `network`:
|
||||
|
||||
Filename: src/lib.rs
|
||||
|
||||
```rust
|
||||
mod network {
|
||||
fn connect() {
|
||||
|
@ -208,6 +208,8 @@ Overall, these are the rules for item visibility:
|
||||
Let's look at a few more examples to get some practice. What if we had this
|
||||
code in a new project's `src/lib.rs`:
|
||||
|
||||
Filename: src/lib.rs
|
||||
|
||||
```rust,ignore
|
||||
mod outermost {
|
||||
pub fn middle_function() {}
|
||||
|
@ -3,6 +3,8 @@
|
||||
We've seen how we can call functions defined within a module by using the
|
||||
module name as part of the call, like this:
|
||||
|
||||
Filename: src/main.rs
|
||||
|
||||
```rust
|
||||
pub mod a {
|
||||
pub mod series {
|
||||
@ -21,6 +23,8 @@ However, referring to the fully qualified name can get quite lengthy, as we see
|
||||
in that example. To solve this issue, Rust has a keyword, `use`. It works like
|
||||
this:
|
||||
|
||||
Filename: src/main.rs
|
||||
|
||||
```rust
|
||||
pub mod a {
|
||||
pub mod series {
|
||||
@ -132,6 +136,8 @@ Since tests are for exercising the code within our library, let's try to call
|
||||
our `client::connect` function from this `it_works` function, even though
|
||||
we're not going to be checking any functionality right now:
|
||||
|
||||
Filename: src/lib.rs
|
||||
|
||||
```rust
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@ -193,6 +199,8 @@ instead of to the root module.
|
||||
For these reasons, in the `tests` module especially, `use super::something` is
|
||||
usually the way to go. So now our test looks like this:
|
||||
|
||||
Filename: src/lib.rs
|
||||
|
||||
```rust
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
Loading…
Reference in New Issue
Block a user