@@ -618,7 +618,7 @@ each of which may have some number of [attributes](#attributes) attached to it.
618
618
619
619
~~~~~~~~ {.ebnf .gram}
620
620
item : mod_item | fn_item | type_item | struct_item | enum_item
621
- | static_item | trait_item | impl_item | extern_block ;
621
+ | static_item | trait_item | impl_item | foreign_mod_item ;
622
622
~~~~~~~~
623
623
624
624
An _ item_ is a component of a crate; some module items can be defined in crate
@@ -752,11 +752,10 @@ link_attr : ident '=' literal ;
752
752
~~~~~~~~
753
753
754
754
An _ ` extern mod ` declaration_ specifies a dependency on an external crate.
755
- The external crate is then bound into the declaring scope
756
- as the ` ident ` provided in the ` extern_mod_decl ` .
755
+ The external crate is then bound into the declaring scope as the ` ident ` provided in the ` extern_mod_decl ` .
757
756
758
- The external crate is resolved to a specific ` soname ` at compile time,
759
- and a runtime linkage requirement to that ` soname ` is passed to the linker for
757
+ The external crate is resolved to a specific ` soname ` at compile time, and a
758
+ runtime linkage requirement to that ` soname ` is passed to the linker for
760
759
loading at runtime. The ` soname ` is resolved at compile time by scanning the
761
760
compiler's library path and matching the ` link_attrs ` provided in the
762
761
` use_decl ` against any ` #link ` attributes that were declared on the external
@@ -993,10 +992,10 @@ Thus the return type on `f` only needs to reflect the `if` branch of the conditi
993
992
#### Extern functions
994
993
995
994
Extern functions are part of Rust's foreign function interface,
996
- providing the opposite functionality to [ external blocks ] ( #external-blocks ) .
997
- Whereas external blocks allow Rust code to call foreign code,
998
- extern functions with bodies defined in Rust code _ can be called by foreign
999
- code _ . They are defined in the same way as any other Rust function,
995
+ providing the opposite functionality to [ foreign modules ] ( #foreign-modules ) .
996
+ Whereas foreign modules allow Rust code to call foreign code,
997
+ extern functions with bodies defined in Rust code _ can be called by foreign code _ .
998
+ They are defined in the same way as any other Rust function,
1000
999
except that they have the ` extern ` modifier.
1001
1000
1002
1001
~~~
@@ -1012,8 +1011,7 @@ let fptr: *u8 = new_vec;
1012
1011
~~~
1013
1012
1014
1013
The primary motivation for extern functions is
1015
- to create callbacks for foreign functions that expect to receive function
1016
- pointers.
1014
+ to create callbacks for foreign functions that expect to receive function pointers.
1017
1015
1018
1016
### Type definitions
1019
1017
@@ -1310,61 +1308,64 @@ impl Seq<bool> for u32 {
1310
1308
}
1311
1309
~~~~
1312
1310
1313
- ### External blocks
1311
+ ### Foreign modules
1314
1312
1315
1313
~~~ {.ebnf .gram}
1316
- extern_block_item : "extern" '{' extern_block '} ;
1317
- extern_block : [ foreign_fn ] * ;
1314
+ foreign_mod_item : "extern mod" ident '{' foreign_mod '} ;
1315
+ foreign_mod : [ foreign_fn ] * ;
1318
1316
~~~
1319
1317
1320
- External blocks form the basis for Rust's foreign function interface.
1321
- Declarations in an external block describe symbols
1322
- in external, non-Rust libraries.
1323
-
1324
- Functions within external blocks
1325
- are declared in the same way as other Rust functions,
1326
- with the exception that they may not have a body
1327
- and are instead terminated by a semicolon.
1318
+ Foreign modules form the basis for Rust's foreign function interface. A
1319
+ foreign module describes functions in external, non-Rust
1320
+ libraries.
1321
+ Functions within foreign modules are declared in the same way as other Rust functions,
1322
+ with the exception that they may not have a body and are instead terminated by a semicolon.
1328
1323
1329
1324
~~~
1330
1325
# use core::libc::{c_char, FILE};
1331
1326
# #[nolink]
1332
1327
1333
- extern {
1328
+ extern mod c {
1334
1329
fn fopen(filename: *c_char, mode: *c_char) -> *FILE;
1335
1330
}
1336
1331
~~~
1337
1332
1338
- Functions within external blocks may be called by Rust code,
1339
- just like functions defined in Rust.
1340
- The Rust compiler automatically translates
1341
- between the Rust ABI and the foreign ABI.
1333
+ Functions within foreign modules may be called by Rust code, just like functions defined in Rust.
1334
+ The Rust compiler automatically translates between the Rust ABI and the foreign ABI.
1335
+
1336
+ The name of the foreign module has special meaning to the Rust compiler in
1337
+ that it will treat the module name as the name of a library to link to,
1338
+ performing the linking as appropriate for the target platform. The name
1339
+ given for the foreign module will be transformed in a platform-specific way
1340
+ to determine the name of the library. For example, on Linux the name of the
1341
+ foreign module is prefixed with 'lib' and suffixed with '.so', so the
1342
+ foreign mod 'rustrt' would be linked to a library named 'librustrt.so'.
1342
1343
1343
- A number of [ attributes] ( #attributes ) control the behavior of external
1344
- blocks .
1344
+ A number of [ attributes] ( #attributes ) control the behavior of foreign
1345
+ modules .
1345
1346
1346
- By default external blocks assume
1347
- that the library they are calling uses the standard C "cdecl" ABI.
1348
- Other ABIs may be specified using the ` abi ` attribute as in
1347
+ By default foreign modules assume that the library they are calling use the
1348
+ standard C "cdecl" ABI. Other ABIs may be specified using the ` abi `
1349
+ attribute as in
1349
1350
1350
1351
~~~ {.xfail-test}
1351
1352
// Interface to the Windows API
1352
1353
#[abi = "stdcall"]
1353
- extern { }
1354
+ extern mod kernel32 { }
1354
1355
~~~
1355
1356
1356
- The ` link_name ` attribute allows the name of the library to be specified.
1357
+ The ` link_name ` attribute allows the default library naming behavior to
1358
+ be overridden by explicitly specifying the name of the library.
1357
1359
1358
1360
~~~ {.xfail-test}
1359
1361
#[link_name = "crypto"]
1360
- extern { }
1362
+ extern mod mycrypto { }
1361
1363
~~~
1362
1364
1363
- The ` nolink ` attribute tells the Rust compiler
1364
- not to do any linking for the external block.
1365
- This is particularly useful for creating external blocks for libc,
1366
- which tends to not follow standard library naming conventions
1367
- and is linked to all Rust programs anyway.
1365
+ The ` nolink ` attribute tells the Rust compiler not to do any linking for the foreign module.
1366
+ This is particularly useful for creating foreign
1367
+ modules for libc, which tends to not follow standard library naming
1368
+ conventions and is linked to all Rust programs anyway.
1368
1369
1369
1370
## Attributes
1370
1371
@@ -1945,6 +1946,35 @@ fn avg(v: &[float]) -> float {
1945
1946
}
1946
1947
~~~~
1947
1948
1949
+ #### Swap expressions
1950
+
1951
+ A _ swap expression_ consists of an [ lvalue] ( #lvalues-rvalues-and-temporaries ) followed by a bi-directional arrow (` <-> ` ) and another [ lvalue] ( #lvalues-rvalues-and-temporaries ) .
1952
+
1953
+ Evaluating a swap expression causes, as a side effect, the values held in the left-hand-side and right-hand-side [ lvalues] ( #lvalues-rvalues-and-temporaries ) to be exchanged indivisibly.
1954
+
1955
+ Evaluating a swap expression neither changes reference counts,
1956
+ nor deeply copies any owned structure pointed to by the moved [ rvalue] ( #lvalues-rvalues-and-temporaries ) .
1957
+ Instead, the swap expression represents an indivisible * exchange of ownership* ,
1958
+ between the right-hand-side and the left-hand-side of the expression.
1959
+ No allocation or destruction is entailed.
1960
+
1961
+ An example of three different swap expressions:
1962
+
1963
+ ~~~~~~~~
1964
+ # let mut x = &mut [0];
1965
+ # let mut a = &mut [0];
1966
+ # let i = 0;
1967
+ # struct S1 { z: int };
1968
+ # struct S2 { c: int };
1969
+ # let mut y = S1{z: 0};
1970
+ # let mut b = S2{c: 0};
1971
+
1972
+ x <-> a;
1973
+ x[i] <-> a[i];
1974
+ y.z <-> b.c;
1975
+ ~~~~~~~~
1976
+
1977
+
1948
1978
#### Assignment expressions
1949
1979
1950
1980
An _ assignment expression_ consists of an [ lvalue] ( #lvalues-rvalues-and-temporaries ) expression followed by an
1985
2015
== !=
1986
2016
&&
1987
2017
||
1988
- =
2018
+ = <->
1989
2019
~~~~
1990
2020
1991
2021
Operators at the same precedence level are evaluated left-to-right.
0 commit comments