Skip to content

Commit

Permalink
Merge pull request #1369 from xiaochuanyu/extern-crate
Browse files Browse the repository at this point in the history
Update extern crate related sections
  • Loading branch information
marioidival authored Aug 26, 2020
2 parents 80a10e2 + d55f2a8 commit 789a37b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
- [File hierarchy](mod/split.md)

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

- [Cargo](cargo.md)
- [Dependencies](cargo/deps.md)
Expand Down
2 changes: 1 addition & 1 deletion src/crates/lib.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Library
# Creating a Library

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

Expand Down
29 changes: 0 additions & 29 deletions src/crates/link.md

This file was deleted.

27 changes: 27 additions & 0 deletions src/crates/using_lib.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Using a Library

To link a crate to this new library you may use `rustc`'s `--extern` flag. All
of its items will then be imported under a module named the same as the library.
This module generally behaves the same way as any other module.

```rust,ignore
// extern crate rary; // May be required for Rust 2015 edition or earlier
fn main() {
rary::public_function();
// Error! `private_function` is private
//rary::private_function();
rary::indirect_access();
}
```

```txt
# Where library.rlib is the path to the compiled library, assumed that it's
# in the same directory here:
$ rustc executable.rs --extern rary=library.rlib --edition=2018 && ./executable
called rary's `public_function()`
called rary's `indirect_access()`, that
> called rary's `private_function()`
```
2 changes: 0 additions & 2 deletions src/mod/use.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ The `use` declaration can be used to bind a full path to a new name, for easier
access. It is often used like this:

```rust,editable,ignore
// extern crate deeply; // normally, this would exist and not be commented out!
use crate::deeply::nested::{
my_first_function,
my_second_function,
Expand Down
8 changes: 1 addition & 7 deletions src/testing/integration_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Cargo looks for integration tests in `tests` directory next to `src`.
File `src/lib.rs`:

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

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

```rust,ignore
// extern crate we're testing, same as any other code will do.
extern crate adder;
// importing common module.
mod common;
Expand Down

0 comments on commit 789a37b

Please sign in to comment.