Last active
May 8, 2016 09:18
-
-
Save deepak/61a5384a3049e09f42f3f97f0b09d274 to your computer and use it in GitHub Desktop.
extern and use in rust guessing-game doc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
as a rust noob and coming from ruby | |
was confused why we need `extern crate` and `use` both ? | |
the `guessing-game` chapter has these two paragraphs: | |
``` | |
The first thing we’ve done is change the first line. It now says | |
`extern crate rand`. Because we declared `rand` in our `[dependencies]`, we | |
can use `extern crate` to let Rust know we’ll be making use of it. This also | |
does the equivalent of a `use rand;` as well, so we can make use of anything | |
in the `rand` crate by prefixing it with `rand::`. | |
Next, we added another `use` line: `use rand::Rng`. We’re going to use a | |
method in a moment, and it requires that `Rng` be in scope to work. The basic | |
idea is this: methods are defined on something called ‘traits’, and for the | |
method to work, it needs the trait to be in scope. For more about the | |
details, read the [traits][traits] section. | |
[traits]: traits.html | |
``` | |
But the example does not compile if we remove `use rand::Rng;` | |
also is not clear to me why we need to get a copy of the random number generator without reading the | |
chapter on concurrency. the relevant paragraph is: | |
``` | |
We use the `rand::thread_rng()` function to get a copy of the random number | |
generator, which is local to the particular [thread][concurrency] of execution | |
we’re in. Because we `use rand::Rng`’d above, it has a `gen_range()` method | |
available. This method takes two arguments, and generates a number between | |
them. It’s inclusive on the lower bound, but exclusive on the upper bound, | |
so we need `1` and `101` to get a number ranging from one to a hundred. | |
[concurrency]: concurrency.html | |
``` | |
can this be clarified or summarized without reading the chapter on traits or concurrency ? | |
had this question after doing the `guessing-game` tutorial | |
asked on irc and found that | |
- `extern crate` links the crate | |
- `use` makes the compiler look for that symbol (not sure) | |
then another question was, why not import everything in `Cargo.toml` ? | |
and why have `extern crate` on a file by file basic ? | |
the answer is that we can depend on different versions of a crate | |
EDIT: | |
use is for convenience evidently. so that we do not have to write fully qualified name | |
nothing to do with preludes. thought initially we import all symbols (imps etc) manually | |
or use a predule. nothing like that though evidently | |
more info (from irc) | |
- https://doc.rust-lang.org/book/crates-and-modules.html#importing-external-crates | |
- https://doc.rust-lang.org/book/crates-and-modules.html#importing-modules-with-use | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment