mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
Auto merge of #27512 - Manishearth:rollup, r=Manishearth
- Successful merges: #27397, #27398, #27460, #27470, #27491, #27498, #27502 - Failed merges:
This commit is contained in:
commit
4b79add086
@ -79,7 +79,7 @@ Libraries
|
||||
are used by code generators to emit implementations of [`Debug`].
|
||||
* `str` has new [`to_uppercase`][strup] and [`to_lowercase`][strlow]
|
||||
methods that convert case, following Unicode case mapping.
|
||||
* It is now easier to handle to poisoned locks. The [`PoisonError`]
|
||||
* It is now easier to handle poisoned locks. The [`PoisonError`]
|
||||
type, returned by failing lock operations, exposes `into_inner`,
|
||||
`get_ref`, and `get_mut`, which all give access to the inner lock
|
||||
guard, and allow the poisoned lock to continue to operate. The
|
||||
|
@ -98,8 +98,8 @@ use std::io;
|
||||
|
||||
We’ll need to take user input, and then print the result as output. As such, we
|
||||
need the `io` library from the standard library. Rust only imports a few things
|
||||
into every program, [the ‘prelude’][prelude]. If it’s not in the prelude,
|
||||
you’ll have to `use` it directly.
|
||||
by default into every program, [the ‘prelude’][prelude]. If it’s not in the
|
||||
prelude, you’ll have to `use` it directly.
|
||||
|
||||
[prelude]: ../std/prelude/index.html
|
||||
|
||||
|
@ -8,13 +8,13 @@ so it is assumed that Rust projects will use Cargo from the beginning.
|
||||
[cratesio]: http://doc.crates.io
|
||||
|
||||
Cargo manages three things: building your code, downloading the dependencies
|
||||
your code needs, and building those dependencies. At first, your
|
||||
program doesn’t have any dependencies, so we’ll only be using the first part of
|
||||
its functionality. Eventually, we’ll add more. Since we started off by using
|
||||
Cargo, it'll be easy to add later.
|
||||
your code needs, and building those dependencies. At first, your program doesn’t
|
||||
have any dependencies, so we’ll only be using the first part of its
|
||||
functionality. Eventually, we’ll add more. Since we started off by using Cargo,
|
||||
it'll be easy to add later.
|
||||
|
||||
If you installed Rust via the official installers you will also have Cargo. If
|
||||
you installed Rust some other way, you may want to [check the Cargo
|
||||
If we installed Rust via the official installers we will also have Cargo. If we
|
||||
installed Rust some other way, we may want to [check the Cargo
|
||||
README][cargoreadme] for specific instructions about installing it.
|
||||
|
||||
[cargoreadme]: https://github.com/rust-lang/cargo#installing-cargo-from-nightlies
|
||||
@ -23,20 +23,21 @@ README][cargoreadme] for specific instructions about installing it.
|
||||
|
||||
Let’s convert Hello World to Cargo.
|
||||
|
||||
To Cargo-ify our project, we need to do two things: Make a `Cargo.toml`
|
||||
configuration file, and put our source file in the right place. Let's
|
||||
do that part first:
|
||||
To Cargo-ify our project, we need to do three things: Make a `Cargo.toml`
|
||||
configuration file, put our source file in the right place, and get rid of the
|
||||
old executable (`main.exe` on Windows, `main` everywhere else). Let's do that part first:
|
||||
|
||||
```bash
|
||||
$ mkdir src
|
||||
$ mv main.rs src/main.rs
|
||||
$ rm main # or main.exe on Windows
|
||||
```
|
||||
|
||||
Note that since we're creating an executable, we used `main.rs`. If we
|
||||
want to make a library instead, we should use `lib.rs`. This convention is required
|
||||
for Cargo to successfully compile our projects, but it can be overridden if we wish.
|
||||
Custom file locations for the entry point can be specified
|
||||
with a [`[lib]` or `[[bin]]`][crates-custom] key in the TOML file.
|
||||
Note that since we're creating an executable, we retain `main.rs` as the source
|
||||
filename. If we want to make a library instead, we should use `lib.rs`. This
|
||||
convention is used by Cargo to successfully compile our projects, but it can be
|
||||
overridden if we wish. Custom file locations for the entry point can be
|
||||
specified with a [`[lib]` or `[[bin]]`][crates-custom] key in the TOML file.
|
||||
|
||||
[crates-custom]: http://doc.crates.io/manifest.html#configuring-a-target
|
||||
|
||||
@ -63,8 +64,8 @@ version = "0.0.1"
|
||||
authors = [ "Your name <you@example.com>" ]
|
||||
```
|
||||
|
||||
This file is in the [TOML][toml] format. TOML is similar to INI, but has some
|
||||
extra goodies. According to the TOML docs,
|
||||
This file is in the [TOML][toml] format. TOML is similar to INI, but has some
|
||||
extra goodies. According to the TOML docs,
|
||||
|
||||
> TOML aims to be a minimal configuration file format that's easy to read due
|
||||
> to obvious semantics. TOML is designed to map unambiguously to a hash table.
|
||||
@ -73,7 +74,8 @@ extra goodies. According to the TOML docs,
|
||||
|
||||
[toml]: https://github.com/toml-lang/toml
|
||||
|
||||
Once you have this file in place, we should be ready to build! To do so, run:
|
||||
Once we have this file in place in our project's root directory, we should be
|
||||
ready to build! To do so, run:
|
||||
|
||||
```bash
|
||||
$ cargo build
|
||||
|
@ -112,26 +112,55 @@ match x {
|
||||
}
|
||||
```
|
||||
|
||||
# Ignoring variants
|
||||
# Ignoring bindings
|
||||
|
||||
If you’re matching on an enum which has variants, you can use `..` to
|
||||
ignore the value and type in the variant:
|
||||
You can use `_` in a pattern to disregard the type and value.
|
||||
For example, here’s a `match` against a `Result<T, E>`:
|
||||
|
||||
```rust
|
||||
enum OptionalInt {
|
||||
Value(i32),
|
||||
Missing,
|
||||
}
|
||||
|
||||
let x = OptionalInt::Value(5);
|
||||
|
||||
match x {
|
||||
OptionalInt::Value(..) => println!("Got an int!"),
|
||||
OptionalInt::Missing => println!("No such luck."),
|
||||
# let some_value: Result<i32, &'static str> = Err("There was an error");
|
||||
match some_value {
|
||||
Ok(value) => println!("got a value: {}", value),
|
||||
Err(_) => println!("an error occurred"),
|
||||
}
|
||||
```
|
||||
|
||||
This prints `Got an int!`.
|
||||
In the first arm, we bind the value inside the `Ok` variant to `value`. But
|
||||
in the `Err` arm, we use `_` to disregard the specific error, and just print
|
||||
a general error message.
|
||||
|
||||
`_` is valid in any pattern that creates a binding. This can be useful to
|
||||
ignore parts of a larger structure:
|
||||
|
||||
```rust
|
||||
fn coordinate() -> (i32, i32, i32) {
|
||||
// generate and return some sort of triple tuple
|
||||
# (1, 2, 3)
|
||||
}
|
||||
|
||||
let (x, _, z) = coordinate();
|
||||
```
|
||||
|
||||
Here, we bind the first and last element of the tuple to `x` and `z`, but
|
||||
ignore the middle element.
|
||||
|
||||
Similarly, you can use `..` in a pattern to disregard multiple values.
|
||||
|
||||
```rust
|
||||
enum OptionalTuple {
|
||||
Value(i32, i32, i32),
|
||||
Missing,
|
||||
}
|
||||
|
||||
let x = OptionalTuple::Value(5, -2, 3);
|
||||
|
||||
match x {
|
||||
OptionalTuple::Value(..) => println!("Got a tuple!"),
|
||||
OptionalTuple::Missing => println!("No such luck."),
|
||||
}
|
||||
```
|
||||
|
||||
This prints `Got a tuple!`.
|
||||
|
||||
# Guards
|
||||
|
||||
@ -282,38 +311,6 @@ This ‘destructuring’ behavior works on any compound data type, like
|
||||
[tuples]: primitive-types.html#tuples
|
||||
[enums]: enums.html
|
||||
|
||||
# Ignoring bindings
|
||||
|
||||
You can use `_` in a pattern to disregard the value. For example, here’s a
|
||||
`match` against a `Result<T, E>`:
|
||||
|
||||
```rust
|
||||
# let some_value: Result<i32, &'static str> = Err("There was an error");
|
||||
match some_value {
|
||||
Ok(value) => println!("got a value: {}", value),
|
||||
Err(_) => println!("an error occurred"),
|
||||
}
|
||||
```
|
||||
|
||||
In the first arm, we bind the value inside the `Ok` variant to `value`. But
|
||||
in the `Err` arm, we use `_` to disregard the specific error, and just print
|
||||
a general error message.
|
||||
|
||||
`_` is valid in any pattern that creates a binding. This can be useful to
|
||||
ignore parts of a larger structure:
|
||||
|
||||
```rust
|
||||
fn coordinate() -> (i32, i32, i32) {
|
||||
// generate and return some sort of triple tuple
|
||||
# (1, 2, 3)
|
||||
}
|
||||
|
||||
let (x, _, z) = coordinate();
|
||||
```
|
||||
|
||||
Here, we bind the first and last element of the tuple to `x` and `z`, but
|
||||
ignore the middle element.
|
||||
|
||||
# Mix and Match
|
||||
|
||||
Whew! That’s a lot of different ways to match things, and they can all be
|
||||
|
@ -322,20 +322,16 @@ pub unsafe fn dropped<T>() -> T {
|
||||
/// println!("{:?}", &data[0]);
|
||||
/// ```
|
||||
///
|
||||
/// Hopefully this example emphasizes to you exactly how delicate
|
||||
/// and dangerous doing this is. Note that the `vec!` macro
|
||||
/// *does* let you initialize every element with a value that
|
||||
/// is only `Clone`, so the following is equivalent and vastly
|
||||
/// less dangerous, as long as you can live with an extra heap
|
||||
/// This example emphasizes exactly how delicate and dangerous doing this is.
|
||||
/// Note that the `vec!` macro *does* let you initialize every element with a
|
||||
/// value that is only `Clone`, so the following is semantically equivalent and
|
||||
/// vastly less dangerous, as long as you can live with an extra heap
|
||||
/// allocation:
|
||||
///
|
||||
/// ```
|
||||
/// let data: Vec<Vec<u32>> = vec![Vec::new(); 1000];
|
||||
/// println!("{:?}", &data[0]);
|
||||
/// ```
|
||||
///
|
||||
/// For large arrays this is probably advisable
|
||||
/// anyway to avoid blowing the stack.
|
||||
#[inline]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
pub unsafe fn uninitialized<T>() -> T {
|
||||
|
@ -313,8 +313,8 @@ fn resolve_error<'b, 'a:'b, 'tcx:'a>(resolver: &'b Resolver<'a, 'tcx>, span: syn
|
||||
},
|
||||
ResolutionError::StructVariantUsedAsFunction(path_name) => {
|
||||
span_err!(resolver.session, span, E0423,
|
||||
"`{}` is a struct variant name, but \
|
||||
this expression \
|
||||
"`{}` is the name of a struct or struct variant, \
|
||||
but this expression \
|
||||
uses it like a function name",
|
||||
path_name);
|
||||
},
|
||||
|
@ -826,6 +826,63 @@ struct Foo { x: Option<Box<Foo>> }
|
||||
Now it's possible to create at least one instance of `Foo`: `Foo { x: None }`.
|
||||
"##,
|
||||
|
||||
E0074: r##"
|
||||
When using the `#[simd]` attribute on a tuple struct, the components of the
|
||||
tuple struct must all be of a concrete, nongeneric type so the compiler can
|
||||
reason about how to use SIMD with them. This error will occur if the types
|
||||
are generic.
|
||||
|
||||
```
|
||||
#[simd]
|
||||
struct Bad<T>(T, T, T); // This will cause an error
|
||||
|
||||
#[simd]
|
||||
struct Good(u32, u32, u32); // This will not
|
||||
```
|
||||
"##,
|
||||
|
||||
E0075: r##"
|
||||
The `#[simd]` attribute can only be applied to non empty tuple structs, because
|
||||
it doesn't make sense to try to use SIMD operations when there are no values to
|
||||
operate on.
|
||||
|
||||
```
|
||||
#[simd]
|
||||
struct Bad; // This will cause an error
|
||||
|
||||
#[simd]
|
||||
struct Good(u32); // This will not
|
||||
```
|
||||
"##,
|
||||
|
||||
E0076: r##"
|
||||
When using the `#[simd]` attribute to automatically use SIMD operations in tuple
|
||||
struct, the types in the struct must all be of the same type, or the compiler
|
||||
will trigger this error.
|
||||
|
||||
```
|
||||
#[simd]
|
||||
struct Bad(u16, u32, u32); // This will cause an error
|
||||
|
||||
#[simd]
|
||||
struct Good(u32, u32, u32); // This will not
|
||||
```
|
||||
|
||||
"##,
|
||||
|
||||
E0077: r##"
|
||||
When using the `#[simd]` attribute on a tuple struct, the elements in the tuple
|
||||
must be machine types so SIMD operations can be applied to them.
|
||||
|
||||
```
|
||||
#[simd]
|
||||
struct Bad(String); // This will cause an error
|
||||
|
||||
#[simd]
|
||||
struct Good(u32, u32, u32); // This will not
|
||||
```
|
||||
"##,
|
||||
|
||||
E0081: r##"
|
||||
Enum discriminants are used to differentiate enum variants stored in memory.
|
||||
This error indicates that the same value was used for two or more variants,
|
||||
@ -2378,10 +2435,6 @@ https://doc.rust-lang.org/std/marker/struct.PhantomData.html
|
||||
|
||||
register_diagnostics! {
|
||||
E0068,
|
||||
E0074,
|
||||
E0075,
|
||||
E0076,
|
||||
E0077,
|
||||
E0085,
|
||||
E0086,
|
||||
E0090,
|
||||
|
@ -11,8 +11,8 @@
|
||||
//! The Rust Prelude
|
||||
//!
|
||||
//! Because `std` is required by most serious Rust software, it is
|
||||
//! imported at the topmost level of every crate by default, as if the
|
||||
//! first line of each crate was
|
||||
//! imported at the topmost level of every crate by default, as if
|
||||
//! each crate contains the following:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! extern crate std;
|
||||
@ -23,7 +23,7 @@
|
||||
//! etc.
|
||||
//!
|
||||
//! Additionally, `std` contains a versioned *prelude* that reexports many of the
|
||||
//! most common traits, types and functions. *The contents of the prelude are
|
||||
//! most common traits, types, and functions. *The contents of the prelude are
|
||||
//! imported into every module by default*. Implicitly, all modules behave as if
|
||||
//! they contained the following [`use` statement][book-use]:
|
||||
//!
|
||||
|
@ -14,6 +14,6 @@ struct Monster {
|
||||
|
||||
|
||||
fn main() {
|
||||
let _m = Monster(); //~ ERROR `Monster` is a struct variant name, but
|
||||
let _m = Monster(); //~ ERROR `Monster` is the name of a struct or
|
||||
//~^ HELP did you mean to write: `Monster { /* fields */ }`?
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user