@@ -973,8 +973,7 @@ Use declarations support a number of convenient shortcuts:
973
973
974
974
An example of ` use ` declarations:
975
975
976
- ```
977
- # #![feature(core)]
976
+ ``` rust
978
977
use std :: option :: Option :: {Some , None };
979
978
use std :: collections :: hash_map :: {self , HashMap };
980
979
@@ -1031,16 +1030,17 @@ declarations.
1031
1030
An example of what will and will not work for ` use ` items:
1032
1031
1033
1032
```
1034
- # #![feature(core)]
1035
1033
# #![allow(unused_imports)]
1036
- use foo::core::iter; // good: foo is at the root of the crate
1037
1034
use foo::baz::foobaz; // good: foo is at the root of the crate
1038
1035
1039
1036
mod foo {
1040
- extern crate core;
1041
1037
1042
- use foo::core::iter; // good: foo is at crate root
1043
- // use core::iter; // bad: core is not at the crate root
1038
+ mod example {
1039
+ pub mod iter {}
1040
+ }
1041
+
1042
+ use foo::example::iter; // good: foo is at crate root
1043
+ // use example::iter; // bad: core is not at the crate root
1044
1044
use self::baz::foobaz; // good: self refers to module 'foo'
1045
1045
use foo::bar::foobar; // good: foo is at crate root
1046
1046
@@ -1368,17 +1368,14 @@ a = Animal::Cat;
1368
1368
1369
1369
Enumeration constructors can have either named or unnamed fields:
1370
1370
1371
- ```
1372
- # #![feature(struct_variant)]
1373
- # fn main() {
1371
+ ``` rust
1374
1372
enum Animal {
1375
1373
Dog (String , f64 ),
1376
1374
Cat { name : String , weight : f64 }
1377
1375
}
1378
1376
1379
1377
let mut a : Animal = Animal :: Dog (" Cocoa" . to_string (), 37.2 );
1380
1378
a = Animal :: Cat { name : " Spotty" . to_string (), weight : 2.7 };
1381
- # }
1382
1379
```
1383
1380
1384
1381
In this example, ` Cat ` is a _ struct-like enum variant_ ,
@@ -1718,17 +1715,6 @@ Functions within external blocks are declared in the same way as other Rust
1718
1715
functions, with the exception that they may not have a body and are instead
1719
1716
terminated by a semicolon.
1720
1717
1721
- ```
1722
- # #![feature(libc)]
1723
- extern crate libc;
1724
- use libc::{c_char, FILE};
1725
-
1726
- extern {
1727
- fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE;
1728
- }
1729
- # fn main() {}
1730
- ```
1731
-
1732
1718
Functions within external blocks may be called by Rust code, just like
1733
1719
functions defined in Rust. The Rust compiler automatically translates between
1734
1720
the Rust ABI and the foreign ABI.
@@ -1739,7 +1725,7 @@ By default external blocks assume that the library they are calling uses the
1739
1725
standard C "cdecl" ABI. Other ABIs may be specified using an ` abi ` string, as
1740
1726
shown here:
1741
1727
1742
- ``` {. ignore}
1728
+ ``` ignore
1743
1729
// Interface to the Windows API
1744
1730
extern "stdcall" { }
1745
1731
```
@@ -3231,55 +3217,7 @@ expression.
3231
3217
3232
3218
In a pattern whose head expression has an ` enum ` type, a placeholder (` _ ` )
3233
3219
stands for a * single* data field, whereas a wildcard ` .. ` stands for * all* the
3234
- fields of a particular variant. For example:
3235
-
3236
- ```
3237
- #![feature(box_patterns)]
3238
- #![feature(box_syntax)]
3239
- enum List<X> { Nil, Cons(X, Box<List<X>>) }
3240
-
3241
- fn main() {
3242
- let x: List<i32> = List::Cons(10, box List::Cons(11, box List::Nil));
3243
-
3244
- match x {
3245
- List::Cons(_, box List::Nil) => panic!("singleton list"),
3246
- List::Cons(..) => return,
3247
- List::Nil => panic!("empty list")
3248
- }
3249
- }
3250
- ```
3251
-
3252
- The first pattern matches lists constructed by applying ` Cons ` to any head
3253
- value, and a tail value of ` box Nil ` . The second pattern matches _ any_ list
3254
- constructed with ` Cons ` , ignoring the values of its arguments. The difference
3255
- between ` _ ` and ` .. ` is that the pattern ` C(_) ` is only type-correct if ` C ` has
3256
- exactly one argument, while the pattern ` C(..) ` is type-correct for any enum
3257
- variant ` C ` , regardless of how many arguments ` C ` has.
3258
-
3259
- Used inside an array pattern, ` .. ` stands for any number of elements, when the
3260
- ` advanced_slice_patterns ` feature gate is turned on. This wildcard can be used
3261
- at most once for a given array, which implies that it cannot be used to
3262
- specifically match elements that are at an unknown distance from both ends of a
3263
- array, like ` [.., 42, ..] ` . If preceded by a variable name, it will bind the
3264
- corresponding slice to the variable. Example:
3265
-
3266
- ```
3267
- # #![feature(advanced_slice_patterns, slice_patterns)]
3268
- fn is_symmetric(list: &[u32]) -> bool {
3269
- match list {
3270
- [] | [_] => true,
3271
- [x, inside.., y] if x == y => is_symmetric(inside),
3272
- _ => false
3273
- }
3274
- }
3275
-
3276
- fn main() {
3277
- let sym = &[0, 1, 4, 2, 4, 1, 0];
3278
- let not_sym = &[0, 1, 7, 2, 4, 1, 0];
3279
- assert!(is_symmetric(sym));
3280
- assert!(!is_symmetric(not_sym));
3281
- }
3282
- ```
3220
+ fields of a particular variant.
3283
3221
3284
3222
A ` match ` behaves differently depending on whether or not the head expression
3285
3223
is an [ lvalue or an rvalue] ( #lvalues,-rvalues-and-temporaries ) . If the head
@@ -3298,30 +3236,15 @@ the inside of the match.
3298
3236
An example of a ` match ` expression:
3299
3237
3300
3238
```
3301
- #![feature(box_patterns)]
3302
- #![feature(box_syntax)]
3303
- # fn process_pair(a: i32, b: i32) { }
3304
- # fn process_ten() { }
3305
-
3306
- enum List<X> { Nil, Cons(X, Box<List<X>>) }
3307
-
3308
- fn main() {
3309
- let x: List<i32> = List::Cons(10, box List::Cons(11, box List::Nil));
3239
+ let x = 1;
3310
3240
3311
- match x {
3312
- List::Cons(a, box List::Cons(b, _)) => {
3313
- process_pair(a, b);
3314
- }
3315
- List::Cons(10, _) => {
3316
- process_ten();
3317
- }
3318
- List::Nil => {
3319
- return;
3320
- }
3321
- _ => {
3322
- panic!();
3323
- }
3324
- }
3241
+ match x {
3242
+ 1 => println!("one"),
3243
+ 2 => println!("two"),
3244
+ 3 => println!("three"),
3245
+ 4 => println!("four"),
3246
+ 5 => println!("five"),
3247
+ _ => println!("something else"),
3325
3248
}
3326
3249
```
3327
3250
@@ -3334,28 +3257,12 @@ Subpatterns can also be bound to variables by the use of the syntax `variable @
3334
3257
subpattern`. For example:
3335
3258
3336
3259
```
3337
- #![feature(box_patterns)]
3338
- #![feature(box_syntax)]
3339
-
3340
- enum List { Nil, Cons(u32, Box<List>) }
3260
+ let x = 1;
3341
3261
3342
- fn is_sorted(list: &List) -> bool {
3343
- match *list {
3344
- List::Nil | List::Cons(_, box List::Nil) => true,
3345
- List::Cons(x, ref r @ box List::Cons(_, _)) => {
3346
- match *r {
3347
- box List::Cons(y, _) => (x <= y) && is_sorted(&**r),
3348
- _ => panic!()
3349
- }
3350
- }
3351
- }
3352
- }
3353
-
3354
- fn main() {
3355
- let a = List::Cons(6, box List::Cons(7, box List::Cons(42, box List::Nil)));
3356
- assert!(is_sorted(&a));
3262
+ match x {
3263
+ e @ 1 ... 5 => println!("got a range element {}", e),
3264
+ _ => println!("anything"),
3357
3265
}
3358
-
3359
3266
```
3360
3267
3361
3268
Patterns can also dereference pointers by using the ` & ` , ` &mut ` and ` box `
0 commit comments