doc: "import" -> "use"
This commit is contained in:
parent
f686896f60
commit
10c533861b
67
doc/rust.md
67
doc/rust.md
|
@ -203,7 +203,7 @@ grammar as double-quoted strings. Other tokens have exact rules given.
|
|||
The keywords in [crate files](#crate-files) are the following strings:
|
||||
|
||||
~~~~~~~~ {.keyword}
|
||||
import export use mod
|
||||
export use mod
|
||||
~~~~~~~~
|
||||
|
||||
The keywords in [source files](#source-files) are the following strings:
|
||||
|
@ -215,7 +215,7 @@ check const copy
|
|||
drop
|
||||
else enum export extern
|
||||
fail false fn for
|
||||
if impl import
|
||||
if impl
|
||||
let log loop
|
||||
match mod mut
|
||||
pure
|
||||
|
@ -447,7 +447,7 @@ expression context, the final namespace qualifier is omitted.
|
|||
Two examples of paths with type arguments:
|
||||
|
||||
~~~~
|
||||
# import std::map;
|
||||
# use std::map;
|
||||
# fn f() {
|
||||
# fn id<T:copy>(t: T) -> T { t }
|
||||
type t = map::hashmap<int,~str>; // Type arguments used in a type expression
|
||||
|
@ -619,8 +619,8 @@ or a *configuration* in Mesa.] A crate file describes:
|
|||
and copyright. These are used for linking, versioning and distributing
|
||||
crates.
|
||||
* The source-file and directory modules that make up the crate.
|
||||
* Any `use`, `import` or `export` [view items](#view-items) that apply to the
|
||||
anonymous module at the top-level of the crate's module tree.
|
||||
* Any `use`, `extern mod` or `export` [view items](#view-items) that apply to
|
||||
the anonymous module at the top-level of the crate's module tree.
|
||||
|
||||
An example of a crate file:
|
||||
|
||||
|
@ -636,7 +636,7 @@ An example of a crate file:
|
|||
author = "Jane Doe" ];
|
||||
|
||||
// Import a module.
|
||||
use std (ver = "1.0");
|
||||
extern mod std (ver = "1.0");
|
||||
|
||||
// Define some modules.
|
||||
#[path = "foo.rs"]
|
||||
|
@ -767,28 +767,28 @@ mod math {
|
|||
#### View items
|
||||
|
||||
~~~~~~~~ {.ebnf .gram}
|
||||
view_item : use_decl | import_decl | export_decl ;
|
||||
view_item : extern_mod_decl | use_decl | export_decl ;
|
||||
~~~~~~~~
|
||||
|
||||
A view item manages the namespace of a module; it does not define new items
|
||||
but simply changes the visibility of other items. There are several kinds of
|
||||
view item:
|
||||
|
||||
* [extern mod declarations](#extern-mod-declarations)
|
||||
* [use declarations](#use-declarations)
|
||||
* [import declarations](#import-declarations)
|
||||
* [export declarations](#export-declarations)
|
||||
|
||||
##### Use declarations
|
||||
##### Extern mod declarations
|
||||
|
||||
~~~~~~~~ {.ebnf .gram}
|
||||
use_decl : "use" ident [ '(' link_attrs ')' ] ? ;
|
||||
extern_mod_decl : "extern" "mod" ident [ '(' link_attrs ')' ] ? ;
|
||||
link_attrs : link_attr [ ',' link_attrs ] + ;
|
||||
link_attr : ident '=' literal ;
|
||||
~~~~~~~~
|
||||
|
||||
A _use declaration_ specifies a dependency on an external crate. The external
|
||||
crate is then imported into the declaring scope as the `ident` provided in the
|
||||
`use_decl`.
|
||||
An _extern mod declaration_ specifies a dependency on an external crate. The
|
||||
external crate is then imported into the declaring scope as the `ident`
|
||||
provided in the `extern_mod_decl`.
|
||||
|
||||
The external crate is resolved to a specific `soname` at compile time, and a
|
||||
runtime linkage requirement to that `soname` is passed to the linker for
|
||||
|
@ -798,51 +798,52 @@ compiler's library path and matching the `link_attrs` provided in the
|
|||
crate when it was compiled. If no `link_attrs` are provided, a default `name`
|
||||
attribute is assumed, equal to the `ident` given in the `use_decl`.
|
||||
|
||||
Two examples of `use` declarations:
|
||||
Two examples of `extern mod` declarations:
|
||||
|
||||
~~~~~~~~{.xfail-test}
|
||||
use pcre (uuid = "54aba0f8-a7b1-4beb-92f1-4cf625264841");
|
||||
extern mod pcre (uuid = "54aba0f8-a7b1-4beb-92f1-4cf625264841");
|
||||
|
||||
use std; // equivalent to: use std ( name = "std" );
|
||||
extern mod std; // equivalent to: extern mod std ( name = "std" );
|
||||
|
||||
use ruststd (name = "std"); // linking to 'std' under another name
|
||||
extern mod ruststd (name = "std"); // linking to 'std' under another name
|
||||
~~~~~~~~
|
||||
|
||||
##### Import declarations
|
||||
##### Use declarations
|
||||
|
||||
~~~~~~~~ {.ebnf .gram}
|
||||
import_decl : "import" ident [ '=' path
|
||||
| "::" path_glob ] ;
|
||||
use_decl : "use" ident [ '=' path
|
||||
| "::" path_glob ] ;
|
||||
|
||||
path_glob : ident [ "::" path_glob ] ?
|
||||
| '*'
|
||||
| '{' ident [ ',' ident ] * '}'
|
||||
~~~~~~~~
|
||||
|
||||
An _import declaration_ creates one or more local name bindings synonymous
|
||||
with some other [path](#paths). Usually an import declaration is used to
|
||||
A _use declaration_ creates one or more local name bindings synonymous
|
||||
with some other [path](#paths). Usually an use declaration is used to
|
||||
shorten the path required to refer to a module item.
|
||||
|
||||
*Note*: unlike many languages, Rust's `import` declarations do *not* declare
|
||||
*Note*: unlike many languages, Rust's `use` declarations do *not* declare
|
||||
linkage-dependency with external crates. Linkage dependencies are
|
||||
independently declared with [`use` declarations](#use-declarations).
|
||||
independently declared with
|
||||
[`extern mod` declarations](#extern-mod-declarations).
|
||||
|
||||
Imports support a number of "convenience" notations:
|
||||
|
||||
* Importing as a different name than the imported name, using the
|
||||
syntax `import x = p::q::r;`.
|
||||
syntax `use x = p::q::r;`.
|
||||
* Importing a list of paths differing only in final element, using
|
||||
the glob-like brace syntax `import a::b::{c,d,e,f};`
|
||||
the glob-like brace syntax `use a::b::{c,d,e,f};`
|
||||
* Importing all paths matching a given prefix, using the glob-like
|
||||
asterisk syntax `import a::b::*;`
|
||||
asterisk syntax `use a::b::*;`
|
||||
|
||||
An example of imports:
|
||||
|
||||
~~~~
|
||||
import foo = core::info;
|
||||
import core::float::sin;
|
||||
import core::str::{slice, to_upper};
|
||||
import core::option::Some;
|
||||
use foo = core::info;
|
||||
use core::float::sin;
|
||||
use core::str::{slice, to_upper};
|
||||
use core::option::Some;
|
||||
|
||||
fn main() {
|
||||
// Equivalent to 'log(core::info, core::float::sin(1.0));'
|
||||
|
@ -1053,7 +1054,7 @@ verify the semantics of the pure functions they write.
|
|||
An example of a pure function that uses an unchecked block:
|
||||
|
||||
~~~~
|
||||
# import std::list::*;
|
||||
# use std::list::*;
|
||||
|
||||
fn pure_foldl<T, U: copy>(ls: List<T>, u: U, f: fn(&&T, &&U) -> U) -> U {
|
||||
match ls {
|
||||
|
@ -1347,7 +1348,7 @@ Rust functions, with the exception that they may not have a body and are
|
|||
instead terminated by a semi-colon.
|
||||
|
||||
~~~
|
||||
# import libc::{c_char, FILE};
|
||||
# use libc::{c_char, FILE};
|
||||
# #[nolink]
|
||||
|
||||
extern mod c {
|
||||
|
|
|
@ -13,8 +13,8 @@ hexadecimal string and prints to standard output. If you have the
|
|||
OpenSSL libraries installed, it should 'just work'.
|
||||
|
||||
~~~~ {.xfail-test}
|
||||
use std;
|
||||
import libc::c_uint;
|
||||
extern mod std;
|
||||
use libc::c_uint;
|
||||
|
||||
extern mod crypto {
|
||||
fn SHA1(src: *u8, sz: c_uint, out: *u8) -> *u8;
|
||||
|
@ -208,8 +208,8 @@ This program uses the POSIX function `gettimeofday` to get a
|
|||
microsecond-resolution timer.
|
||||
|
||||
~~~~
|
||||
use std;
|
||||
import libc::c_ulonglong;
|
||||
extern mod std;
|
||||
use libc::c_ulonglong;
|
||||
|
||||
type timeval = {mut tv_sec: c_ulonglong,
|
||||
mut tv_usec: c_ulonglong};
|
||||
|
|
|
@ -1222,7 +1222,7 @@ most vector functionality is provided by methods, so let's have a
|
|||
brief look at a few common ones.
|
||||
|
||||
~~~
|
||||
# import io::println;
|
||||
# use io::println;
|
||||
# enum crayon {
|
||||
# almond, antique_brass, apricot,
|
||||
# aquamarine, asparagus, atomic_tangerine,
|
||||
|
@ -1276,7 +1276,7 @@ Rust also supports _closures_, functions that can access variables in
|
|||
the enclosing scope.
|
||||
|
||||
~~~~
|
||||
# import println = io::println;
|
||||
# use println = io::println;
|
||||
fn call_closure_with_ten(b: fn(int)) { b(10); }
|
||||
|
||||
let captured_var = 20;
|
||||
|
@ -1434,7 +1434,7 @@ takes a final closure argument.
|
|||
`do` is often used for task spawning.
|
||||
|
||||
~~~~
|
||||
import task::spawn;
|
||||
use task::spawn;
|
||||
|
||||
do spawn() || {
|
||||
debug!("I'm a task, whatever");
|
||||
|
@ -1446,7 +1446,7 @@ argument lists back to back. Wouldn't it be great if they weren't
|
|||
there?
|
||||
|
||||
~~~~
|
||||
# import task::spawn;
|
||||
# use task::spawn;
|
||||
do spawn {
|
||||
debug!("Kablam!");
|
||||
}
|
||||
|
@ -1479,8 +1479,8 @@ fn each(v: ~[int], op: fn(int) -> bool) {
|
|||
And using this function to iterate over a vector:
|
||||
|
||||
~~~~
|
||||
# import each = vec::each;
|
||||
# import println = io::println;
|
||||
# use each = vec::each;
|
||||
# use println = io::println;
|
||||
each(~[2, 4, 8, 5, 16], |n| {
|
||||
if n % 2 != 0 {
|
||||
println(~"found odd number!");
|
||||
|
@ -1496,8 +1496,8 @@ out of the loop, you just write `break`. To skip ahead
|
|||
to the next iteration, write `again`.
|
||||
|
||||
~~~~
|
||||
# import each = vec::each;
|
||||
# import println = io::println;
|
||||
# use each = vec::each;
|
||||
# use println = io::println;
|
||||
for each(~[2, 4, 8, 5, 16]) |n| {
|
||||
if n % 2 != 0 {
|
||||
println(~"found odd number!");
|
||||
|
@ -1512,7 +1512,7 @@ normally allowed in closures, in a block that appears as the body of a
|
|||
function, not just the loop body.
|
||||
|
||||
~~~~
|
||||
# import each = vec::each;
|
||||
# use each = vec::each;
|
||||
fn contains(v: ~[int], elt: int) -> bool {
|
||||
for each(v) |x| {
|
||||
if (x == elt) { return true; }
|
||||
|
@ -1760,22 +1760,22 @@ that path is several modules deep). Rust allows you to import
|
|||
identifiers at the top of a file, module, or block.
|
||||
|
||||
~~~~
|
||||
use std;
|
||||
import io::println;
|
||||
extern mod std;
|
||||
use io::println;
|
||||
fn main() {
|
||||
println(~"that was easy");
|
||||
}
|
||||
~~~~
|
||||
|
||||
It is also possible to import just the name of a module (`import
|
||||
It is also possible to import just the name of a module (`use
|
||||
std::list;`, then use `list::find`), to import all identifiers exported
|
||||
by a given module (`import io::*`), or to import a specific set
|
||||
of identifiers (`import math::{min, max, pi}`).
|
||||
by a given module (`use io::*`), or to import a specific set
|
||||
of identifiers (`use math::{min, max, pi}`).
|
||||
|
||||
You can rename an identifier when importing using the `=` operator:
|
||||
|
||||
~~~~
|
||||
import prnt = io::println;
|
||||
use prnt = io::println;
|
||||
~~~~
|
||||
|
||||
## Exporting
|
||||
|
@ -1836,14 +1836,14 @@ fn main() {
|
|||
}
|
||||
~~~~
|
||||
|
||||
An `import` directive will only import into the namespaces for which
|
||||
An `use` directive will only import into the namespaces for which
|
||||
identifiers are actually found. Consider this example:
|
||||
|
||||
~~~~
|
||||
type bar = uint;
|
||||
mod foo { fn bar() {} }
|
||||
mod baz {
|
||||
import foo::bar;
|
||||
use foo::bar;
|
||||
const x: bar = 20u;
|
||||
}
|
||||
~~~~
|
||||
|
@ -2089,8 +2089,8 @@ Spawning a task is done using the various spawn functions in the
|
|||
module `task`. Let's begin with the simplest one, `task::spawn()`:
|
||||
|
||||
~~~~
|
||||
import task::spawn;
|
||||
import io::println;
|
||||
use task::spawn;
|
||||
use io::println;
|
||||
|
||||
let some_value = 22;
|
||||
|
||||
|
@ -2116,8 +2116,8 @@ receiving messages. The easiest way to create a pipe is to use
|
|||
computations in parallel. We might write something like:
|
||||
|
||||
~~~~
|
||||
import task::spawn;
|
||||
import pipes::{stream, Port, Chan};
|
||||
use task::spawn;
|
||||
use pipes::{stream, Port, Chan};
|
||||
|
||||
let (chan, port) = stream();
|
||||
|
||||
|
@ -2137,7 +2137,7 @@ Let's walk through this code line-by-line. The first line creates a
|
|||
stream for sending and receiving integers:
|
||||
|
||||
~~~~ {.ignore}
|
||||
# import pipes::stream;
|
||||
# use pipes::stream;
|
||||
let (chan, port) = stream();
|
||||
~~~~
|
||||
|
||||
|
@ -2146,8 +2146,8 @@ once it is complete. The channel will be used by the child to send a
|
|||
message to the port. The next statement actually spawns the child:
|
||||
|
||||
~~~~
|
||||
# import task::{spawn};
|
||||
# import comm::{Port, Chan};
|
||||
# use task::{spawn};
|
||||
# use comm::{Port, Chan};
|
||||
# fn some_expensive_computation() -> int { 42 }
|
||||
# let port = Port();
|
||||
# let chan = port.chan();
|
||||
|
@ -2167,7 +2167,7 @@ some other expensive computation and then waiting for the child's result
|
|||
to arrive on the port:
|
||||
|
||||
~~~~
|
||||
# import pipes::{stream, Port, Chan};
|
||||
# use pipes::{stream, Port, Chan};
|
||||
# fn some_other_expensive_computation() {}
|
||||
# let (chan, port) = stream::<int>();
|
||||
# chan.send(0);
|
||||
|
@ -2188,8 +2188,8 @@ the string in response. The child terminates when `0` is received.
|
|||
Here is the function that implements the child task:
|
||||
|
||||
~~~~
|
||||
# import std::comm::DuplexStream;
|
||||
# import pipes::{Port, Chan};
|
||||
# use std::comm::DuplexStream;
|
||||
# use pipes::{Port, Chan};
|
||||
fn stringifier(channel: DuplexStream<~str, uint>) {
|
||||
let mut value: uint;
|
||||
loop {
|
||||
|
@ -2211,9 +2211,9 @@ response itself is simply the strified version of the received value,
|
|||
Here is the code for the parent task:
|
||||
|
||||
~~~~
|
||||
# import std::comm::DuplexStream;
|
||||
# import pipes::{Port, Chan};
|
||||
# import task::spawn;
|
||||
# use std::comm::DuplexStream;
|
||||
# use pipes::{Port, Chan};
|
||||
# use task::spawn;
|
||||
# fn stringifier(channel: DuplexStream<~str, uint>) {
|
||||
# let mut value: uint;
|
||||
# loop {
|
||||
|
|
Loading…
Reference in New Issue