Add #![feature] attributes to doctests

This commit is contained in:
Brian Anderson 2015-03-13 15:28:35 -07:00
parent df290f127e
commit e9019101a8
107 changed files with 649 additions and 22 deletions

View file

@ -816,8 +816,7 @@ may optionally begin with any number of `attributes` that apply to the
containing module. Attributes on the anonymous crate module define important
metadata that influences the behavior of the compiler.
```{.rust}
# #![allow(unused_attribute)]
```no_run
// Crate name
#![crate_name = "projx"]
@ -1020,6 +1019,7 @@ Use declarations support a number of convenient shortcuts:
An example of `use` declarations:
```
# #![feature(core)]
use std::iter::range_step;
use std::option::Option::{Some, None};
use std::collections::hash_map::{self, HashMap};
@ -1080,6 +1080,7 @@ declarations.
An example of what will and will not work for `use` items:
```
# #![feature(core)]
# #![allow(unused_imports)]
use foo::core::iter; // good: foo is at the root of the crate
use foo::baz::foobaz; // good: foo is at the root of the crate
@ -1781,6 +1782,7 @@ functions, with the exception that they may not have a body and are instead
terminated by a semicolon.
```
# #![feature(libc)]
extern crate libc;
use libc::{c_char, FILE};

View file

@ -88,6 +88,7 @@ When `guard` goes out of scope, it will block execution until the thread is
finished. If we didn't want this behaviour, we could use `thread::spawn()`:
```
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;
@ -146,6 +147,7 @@ As an example, here is a Rust program that would have a data race in many
languages. It will not compile:
```ignore
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;
@ -185,6 +187,7 @@ only one person at a time can mutate what's inside. For that, we can use the
but for a different reason:
```ignore
# #![feature(old_io, std_misc)]
use std::thread;
use std::old_io::timer;
use std::time::Duration;
@ -229,6 +232,7 @@ guard across thread boundaries, which gives us our error.
We can use `Arc<T>` to fix this. Here's the working version:
```
# #![feature(old_io, std_misc)]
use std::sync::{Arc, Mutex};
use std::thread;
use std::old_io::timer;
@ -254,6 +258,7 @@ handle is then moved into the new thread. Let's examine the body of the
thread more closely:
```
# #![feature(old_io, std_misc)]
# use std::sync::{Arc, Mutex};
# use std::thread;
# use std::old_io::timer;

View file

@ -12,6 +12,7 @@ The following is a minimal example of calling a foreign function which will
compile if snappy is installed:
```no_run
# #![feature(libc)]
extern crate libc;
use libc::size_t;
@ -45,6 +46,7 @@ keeping the binding correct at runtime.
The `extern` block can be extended to cover the entire snappy API:
```no_run
# #![feature(libc)]
extern crate libc;
use libc::{c_int, size_t};
@ -80,6 +82,7 @@ length is number of elements currently contained, and the capacity is the total
the allocated memory. The length is less than or equal to the capacity.
```
# #![feature(libc)]
# extern crate libc;
# use libc::{c_int, size_t};
# unsafe fn snappy_validate_compressed_buffer(_: *const u8, _: size_t) -> c_int { 0 }
@ -104,6 +107,7 @@ required capacity to hold the compressed output. The vector can then be passed t
the true length after compression for setting the length.
```
# #![feature(libc)]
# extern crate libc;
# use libc::{size_t, c_int};
# unsafe fn snappy_compress(a: *const u8, b: size_t, c: *mut u8,
@ -130,6 +134,7 @@ Decompression is similar, because snappy stores the uncompressed size as part of
format and `snappy_uncompressed_length` will retrieve the exact buffer size required.
```
# #![feature(libc)]
# extern crate libc;
# use libc::{size_t, c_int};
# unsafe fn snappy_uncompress(compressed: *const u8,
@ -408,6 +413,7 @@ global state. In order to access these variables, you declare them in `extern`
blocks with the `static` keyword:
```no_run
# #![feature(libc)]
extern crate libc;
#[link(name = "readline")]
@ -426,6 +432,7 @@ interface. To do this, statics can be declared with `mut` so we can mutate
them.
```no_run
# #![feature(libc)]
extern crate libc;
use std::ffi::CString;
@ -458,6 +465,7 @@ calling foreign functions. Some foreign functions, most notably the Windows API,
conventions. Rust provides a way to tell the compiler which convention to use:
```
# #![feature(libc)]
extern crate libc;
#[cfg(all(target_os = "win32", target_arch = "x86"))]

View file

@ -246,6 +246,7 @@ These two basic iterators should serve you well. There are some more
advanced iterators, including ones that are infinite. Like `count`:
```rust
# #![feature(core)]
std::iter::count(1, 5);
```
@ -294,6 +295,7 @@ has no side effect on the original iterator. Let's try it out with our infinite
iterator from before, `count()`:
```rust
# #![feature(core)]
for i in std::iter::count(1, 5).take(5) {
println!("{}", i);
}

View file

@ -23,6 +23,7 @@ the ability to use this *method call syntax* via the `impl` keyword.
Here's how it works:
```{rust}
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
@ -87,6 +88,7 @@ original example, `foo.bar().baz()`? This is called 'method chaining', and we
can do it by returning `self`.
```
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
@ -164,6 +166,7 @@ have method overloading, named arguments, or variable arguments. We employ
the builder pattern instead. It looks like this:
```
# #![feature(core)]
struct Circle {
x: f64,
y: f64,

View file

@ -148,6 +148,7 @@ Rust provides iterators for each of these situations:
Usually, the `graphemes()` method on `&str` is what you want:
```
# #![feature(unicode)]
let s = "u͔n͈̰̎i̙̮͚̦c͚̉o̼̩̰͗d͔̆̓ͥé";
for l in s.graphemes(true) {

View file

@ -5,7 +5,7 @@ we haven't seen before. Here's a simple program that reads some input,
and then prints it back out:
```{rust,ignore}
fn main() {
corefn main() {
println!("Type something!");
let input = std::old_io::stdin().read_line().ok().expect("Failed to read line");
@ -28,6 +28,7 @@ Since writing the fully qualified name all the time is annoying, we can use
the `use` statement to import it in:
```{rust}
# #![feature(old_io)]
use std::old_io::stdin;
stdin();
@ -37,6 +38,7 @@ However, it's considered better practice to not import individual functions, but
to import the module, and only use one level of qualification:
```{rust}
# #![feature(old_io)]
use std::old_io;
old_io::stdin();

View file

@ -546,6 +546,8 @@ is an opaque "black box" to the optimizer and so forces it to consider any
argument as used.
```rust
# #![feature(test)]
extern crate test;
# fn main() {

View file

@ -4,6 +4,7 @@ Do you remember the `impl` keyword, used to call a function with method
syntax?
```{rust}
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
@ -21,6 +22,7 @@ Traits are similar, except that we define a trait with just the method
signature, then implement the trait for that struct. Like this:
```{rust}
# #![feature(core)]
struct Circle {
x: f64,
y: f64,
@ -84,6 +86,7 @@ which implements `HasArea` will have an `.area()` method.
Here's an extended example of how this works:
```{rust}
# #![feature(core)]
trait HasArea {
fn area(&self) -> f64;
}
@ -225,6 +228,7 @@ If we add a `use` line right above `main` and make the right things public,
everything is fine:
```{rust}
# #![feature(core)]
use shapes::HasArea;
mod shapes {
@ -408,6 +412,7 @@ but instead, we found a floating-point variable. We need a different bound. `Flo
to the rescue:
```
# #![feature(std_misc)]
use std::num::Float;
fn inverse<T: Float>(x: T) -> Result<T, String> {
@ -423,6 +428,7 @@ from the `Float` trait. Both `f32` and `f64` implement `Float`, so our function
works just fine:
```
# #![feature(std_misc)]
# use std::num::Float;
# fn inverse<T: Float>(x: T) -> Result<T, String> {
# if x == Float::zero() { return Err("x cannot be zero!".to_string()) }

View file

@ -187,6 +187,7 @@ As an example, we give a reimplementation of owned boxes by wrapping
reimplementation is as safe as the `Box` type.
```
# #![feature(libc)]
#![feature(unsafe_destructor)]
extern crate libc;
@ -443,6 +444,7 @@ The function marked `#[start]` is passed the command line parameters
in the same format as C:
```
# #![feature(libc)]
#![feature(lang_items, start, no_std)]
#![no_std]
@ -470,6 +472,7 @@ correct ABI and the correct name, which requires overriding the
compiler's name mangling too:
```ignore
# #![feature(libc)]
#![feature(no_std)]
#![no_std]
#![no_main]
@ -526,6 +529,7 @@ As an example, here is a program that will calculate the dot product of two
vectors provided from C, using idiomatic Rust practices.
```
# #![feature(libc, core)]
#![feature(lang_items, start, no_std)]
#![no_std]
@ -650,6 +654,7 @@ and one for deallocation. A freestanding program that uses the `Box`
sugar for dynamic allocations via `malloc` and `free`:
```
# #![feature(libc)]
#![feature(lang_items, box_syntax, start, no_std)]
#![no_std]