Skip to content

Commit 789a37b

Browse files
authored
Merge pull request #1369 from xiaochuanyu/extern-crate
Update extern crate related sections
2 parents 80a10e2 + d55f2a8 commit 789a37b

File tree

6 files changed

+31
-41
lines changed

6 files changed

+31
-41
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@
8282
- [File hierarchy](mod/split.md)
8383

8484
- [Crates](crates.md)
85-
- [Library](crates/lib.md)
86-
- [`extern crate`](crates/link.md)
85+
- [Creating a Library](crates/lib.md)
86+
- [Using a Library](crates/using_lib.md)
8787

8888
- [Cargo](cargo.md)
8989
- [Dependencies](cargo/deps.md)

src/crates/lib.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Library
1+
# Creating a Library
22

33
Let's create a library, and then see how to link it to another crate.
44

src/crates/link.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/crates/using_lib.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Using a Library
2+
3+
To link a crate to this new library you may use `rustc`'s `--extern` flag. All
4+
of its items will then be imported under a module named the same as the library.
5+
This module generally behaves the same way as any other module.
6+
7+
```rust,ignore
8+
// extern crate rary; // May be required for Rust 2015 edition or earlier
9+
10+
fn main() {
11+
rary::public_function();
12+
13+
// Error! `private_function` is private
14+
//rary::private_function();
15+
16+
rary::indirect_access();
17+
}
18+
```
19+
20+
```txt
21+
# Where library.rlib is the path to the compiled library, assumed that it's
22+
# in the same directory here:
23+
$ rustc executable.rs --extern rary=library.rlib --edition=2018 && ./executable
24+
called rary's `public_function()`
25+
called rary's `indirect_access()`, that
26+
> called rary's `private_function()`
27+
```

src/mod/use.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ The `use` declaration can be used to bind a full path to a new name, for easier
44
access. It is often used like this:
55

66
```rust,editable,ignore
7-
// extern crate deeply; // normally, this would exist and not be commented out!
8-
97
use crate::deeply::nested::{
108
my_first_function,
119
my_second_function,

src/testing/integration_testing.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Cargo looks for integration tests in `tests` directory next to `src`.
1010
File `src/lib.rs`:
1111

1212
```rust,ignore
13-
// Assume that crate is called adder, will have to extern it in integration test.
13+
// Define this in a crate called `adder`.
1414
pub fn add(a: i32, b: i32) -> i32 {
1515
a + b
1616
}
@@ -19,9 +19,6 @@ pub fn add(a: i32, b: i32) -> i32 {
1919
File with test: `tests/integration_test.rs`:
2020

2121
```rust,ignore
22-
// extern crate we're testing, same as any other code would do.
23-
extern crate adder;
24-
2522
#[test]
2623
fn test_add() {
2724
assert_eq!(adder::add(3, 2), 5);
@@ -66,9 +63,6 @@ pub fn setup() {
6663
File with test: `tests/integration_test.rs`
6764

6865
```rust,ignore
69-
// extern crate we're testing, same as any other code will do.
70-
extern crate adder;
71-
7266
// importing common module.
7367
mod common;
7468

0 commit comments

Comments
 (0)