@@ -973,28 +973,6 @@ Use declarations support a number of convenient shortcuts:
973
973
and their immediate parent module, using the ` self ` keyword, such as
974
974
` use a::b::{self, c, d}; `
975
975
976
- An example of ` use ` declarations:
977
-
978
- ```
979
- # #![feature(core)]
980
- use std::option::Option::{Some, None};
981
- use std::collections::hash_map::{self, HashMap};
982
-
983
- fn foo<T>(_: T){}
984
- fn bar(map1: HashMap<String, usize>, map2: hash_map::HashMap<String, usize>){}
985
-
986
- fn main() {
987
- // Equivalent to 'foo(vec![std::option::Option::Some(1.0f64),
988
- // std::option::Option::None]);'
989
- foo(vec![Some(1.0f64), None]);
990
-
991
- // Both `hash_map` and `HashMap` are in scope.
992
- let map1 = HashMap::new();
993
- let map2 = hash_map::HashMap::new();
994
- bar(map1, map2);
995
- }
996
- ```
997
-
998
976
Like items, ` use ` declarations are private to the containing module, by
999
977
default. Also like items, a ` use ` declaration can be public, if qualified by
1000
978
the ` pub ` keyword. Such a ` use ` declaration serves to _ re-export_ a name. A
@@ -1030,35 +1008,6 @@ parent modules respectively. All rules regarding accessing declared modules in
1030
1008
` use ` declarations applies to both module declarations and ` extern crate `
1031
1009
declarations.
1032
1010
1033
- An example of what will and will not work for ` use ` items:
1034
-
1035
- ```
1036
- # #![feature(core)]
1037
- # #![allow(unused_imports)]
1038
- use foo::core::iter; // good: foo is at the root of the crate
1039
- use foo::baz::foobaz; // good: foo is at the root of the crate
1040
-
1041
- mod foo {
1042
- extern crate core;
1043
-
1044
- use foo::core::iter; // good: foo is at crate root
1045
- // use core::iter; // bad: core is not at the crate root
1046
- use self::baz::foobaz; // good: self refers to module 'foo'
1047
- use foo::bar::foobar; // good: foo is at crate root
1048
-
1049
- pub mod bar {
1050
- pub fn foobar() { }
1051
- }
1052
-
1053
- pub mod baz {
1054
- use super::bar::foobar; // good: super refers to module 'foo'
1055
- pub fn foobaz() { }
1056
- }
1057
- }
1058
-
1059
- fn main() {}
1060
- ```
1061
-
1062
1011
### Functions
1063
1012
1064
1013
A _ function item_ defines a sequence of [ statements] ( #statements ) and an
@@ -1378,16 +1327,13 @@ a = Animal::Cat;
1378
1327
Enumeration constructors can have either named or unnamed fields:
1379
1328
1380
1329
```
1381
- # #![feature(struct_variant)]
1382
- # fn main() {
1383
1330
enum Animal {
1384
1331
Dog (String, f64),
1385
1332
Cat { name: String, weight: f64 }
1386
1333
}
1387
1334
1388
1335
let mut a: Animal = Animal::Dog("Cocoa".to_string(), 37.2);
1389
1336
a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 };
1390
- # }
1391
1337
```
1392
1338
1393
1339
In this example, ` Cat ` is a _ struct-like enum variant_ ,
@@ -1727,17 +1673,6 @@ Functions within external blocks are declared in the same way as other Rust
1727
1673
functions, with the exception that they may not have a body and are instead
1728
1674
terminated by a semicolon.
1729
1675
1730
- ```
1731
- # #![feature(libc)]
1732
- extern crate libc;
1733
- use libc::{c_char, FILE};
1734
-
1735
- extern {
1736
- fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE;
1737
- }
1738
- # fn main() {}
1739
- ```
1740
-
1741
1676
Functions within external blocks may be called by Rust code, just like
1742
1677
functions defined in Rust. The Rust compiler automatically translates between
1743
1678
the Rust ABI and the foreign ABI.
@@ -2339,7 +2274,7 @@ considered a full-fledged language feature.
2339
2274
2340
2275
For this reason, Rust recognizes a special crate-level attribute of the form:
2341
2276
2342
- ``` {. ignore}
2277
+ ``` ignore
2343
2278
#![feature(feature1, feature2, feature3)]
2344
2279
```
2345
2280
@@ -3241,55 +3176,17 @@ expression.
3241
3176
3242
3177
In a pattern whose head expression has an ` enum ` type, a placeholder (` _ ` )
3243
3178
stands for a * single* data field, whereas a wildcard ` .. ` stands for * all* the
3244
- fields of a particular variant. For example:
3245
-
3246
- ```
3247
- #![feature(box_patterns)]
3248
- #![feature(box_syntax)]
3249
- enum List<X> { Nil, Cons(X, Box<List<X>>) }
3250
-
3251
- fn main() {
3252
- let x: List<i32> = List::Cons(10, box List::Cons(11, box List::Nil));
3253
-
3254
- match x {
3255
- List::Cons(_, box List::Nil) => panic!("singleton list"),
3256
- List::Cons(..) => return,
3257
- List::Nil => panic!("empty list")
3258
- }
3259
- }
3260
- ```
3261
-
3262
- The first pattern matches lists constructed by applying ` Cons ` to any head
3263
- value, and a tail value of ` box Nil ` . The second pattern matches _ any_ list
3264
- constructed with ` Cons ` , ignoring the values of its arguments. The difference
3265
- between ` _ ` and ` .. ` is that the pattern ` C(_) ` is only type-correct if ` C ` has
3266
- exactly one argument, while the pattern ` C(..) ` is type-correct for any enum
3267
- variant ` C ` , regardless of how many arguments ` C ` has.
3179
+ fields of a particular variant. The difference between ` _ ` and ` .. ` is that the
3180
+ pattern ` C(_) ` is only type-correct if ` C ` has exactly one argument, while the
3181
+ pattern ` C(..) ` is type-correct for any enum variant ` C ` , regardless of how
3182
+ many arguments ` C ` has.
3268
3183
3269
3184
Used inside an array pattern, ` .. ` stands for any number of elements, when the
3270
3185
` advanced_slice_patterns ` feature gate is turned on. This wildcard can be used
3271
3186
at most once for a given array, which implies that it cannot be used to
3272
3187
specifically match elements that are at an unknown distance from both ends of a
3273
3188
array, like ` [.., 42, ..] ` . If preceded by a variable name, it will bind the
3274
- corresponding slice to the variable. Example:
3275
-
3276
- ```
3277
- # #![feature(advanced_slice_patterns, slice_patterns)]
3278
- fn is_symmetric(list: &[u32]) -> bool {
3279
- match list {
3280
- [] | [_] => true,
3281
- [x, inside.., y] if x == y => is_symmetric(inside),
3282
- _ => false
3283
- }
3284
- }
3285
-
3286
- fn main() {
3287
- let sym = &[0, 1, 4, 2, 4, 1, 0];
3288
- let not_sym = &[0, 1, 7, 2, 4, 1, 0];
3289
- assert!(is_symmetric(sym));
3290
- assert!(!is_symmetric(not_sym));
3291
- }
3292
- ```
3189
+ corresponding slice to the variable.
3293
3190
3294
3191
A ` match ` behaves differently depending on whether or not the head expression
3295
3192
is an [ lvalue or an rvalue] ( #lvalues,-rvalues-and-temporaries ) . If the head
@@ -3305,68 +3202,13 @@ possible, it is preferable to match on lvalues, as the lifetime of these
3305
3202
matches inherits the lifetime of the lvalue, rather than being restricted to
3306
3203
the inside of the match.
3307
3204
3308
- An example of a ` match ` expression:
3309
-
3310
- ```
3311
- #![feature(box_patterns)]
3312
- #![feature(box_syntax)]
3313
- # fn process_pair(a: i32, b: i32) { }
3314
- # fn process_ten() { }
3315
-
3316
- enum List<X> { Nil, Cons(X, Box<List<X>>) }
3317
-
3318
- fn main() {
3319
- let x: List<i32> = List::Cons(10, box List::Cons(11, box List::Nil));
3320
-
3321
- match x {
3322
- List::Cons(a, box List::Cons(b, _)) => {
3323
- process_pair(a, b);
3324
- }
3325
- List::Cons(10, _) => {
3326
- process_ten();
3327
- }
3328
- List::Nil => {
3329
- return;
3330
- }
3331
- _ => {
3332
- panic!();
3333
- }
3334
- }
3335
- }
3336
- ```
3337
-
3338
3205
Patterns that bind variables default to binding to a copy or move of the
3339
3206
matched value (depending on the matched value's type). This can be changed to
3340
3207
bind to a reference by using the ` ref ` keyword, or to a mutable reference using
3341
3208
` ref mut ` .
3342
3209
3343
3210
Subpatterns can also be bound to variables by the use of the syntax `variable @
3344
- subpattern`. For example:
3345
-
3346
- ```
3347
- #![feature(box_patterns)]
3348
- #![feature(box_syntax)]
3349
-
3350
- enum List { Nil, Cons(u32, Box<List>) }
3351
-
3352
- fn is_sorted(list: &List) -> bool {
3353
- match *list {
3354
- List::Nil | List::Cons(_, box List::Nil) => true,
3355
- List::Cons(x, ref r @ box List::Cons(_, _)) => {
3356
- match *r {
3357
- box List::Cons(y, _) => (x <= y) && is_sorted(&**r),
3358
- _ => panic!()
3359
- }
3360
- }
3361
- }
3362
- }
3363
-
3364
- fn main() {
3365
- let a = List::Cons(6, box List::Cons(7, box List::Cons(42, box List::Nil)));
3366
- assert!(is_sorted(&a));
3367
- }
3368
-
3369
- ```
3211
+ subpattern`.
3370
3212
3371
3213
Patterns can also dereference pointers by using the ` & ` , ` &mut ` and ` box `
3372
3214
symbols, as appropriate. For example, these two matches on ` x: &i32 ` are
0 commit comments