in different languages. To keep things simple, we’ll stick to ‘greetings’ and
‘farewells’ as two kinds of phrases, and use English and Japanese (日本語) as
two languages for those phrases to be in. We’ll use this module layout:
+
```text
+-----------+
+---| greetings |
y: i32,
}
-let origin = Point { x: 0, y: 0 };
+let point = Point { x: 2, y: 3 };
-match origin {
+match point {
Point { x, .. } => println!("x is {}", x),
}
```
-This prints `x is 0`.
+This prints `x is 2`.
You can do this kind of match on any member, not only the first:
y: i32,
}
-let origin = Point { x: 0, y: 0 };
+let point = Point { x: 2, y: 3 };
-match origin {
+match point {
Point { y, .. } => println!("y is {}", y),
}
```
-This prints `y is 0`.
+This prints `y is 3`.
This ‘destructuring’ behavior works on any compound data type, like
[tuples][tuples] or [enums][enums].
strong to weak:
```{.text .precedence}
-as
+as :
* / %
+ -
<< >>
&&
||
.. ...
+<-
=
```
let &(ref first_arm_pats, _) = &arms[0];
let first_pat = &first_arm_pats[0];
let span = first_pat.span;
- span_err!(cx.tcx.sess, span, E0162, "irrefutable if-let pattern");
+ struct_span_err!(cx.tcx.sess, span, E0162,
+ "irrefutable if-let pattern")
+ .span_label(span, &format!("irrefutable pattern"))
+ .emit();
printed_if_let_err = true;
}
},
self.convert_angle_bracketed_parameters(rscope, span, decl_generics, data)
}
hir::ParenthesizedParameters(..) => {
- span_err!(tcx.sess, span, E0214,
- "parenthesized parameters may only be used with a trait");
+ struct_span_err!(tcx.sess, span, E0214,
+ "parenthesized parameters may only be used with a trait")
+ .span_label(span, &format!("only traits may use parentheses"))
+ .emit();
+
let ty_param_defs = decl_generics.types.get_slice(TypeSpace);
(Substs::empty(),
ty_param_defs.iter().map(|_| tcx.types.err).collect(),
}
for (trait_def_id, name) in associated_types {
- span_err!(tcx.sess, span, E0191,
+ struct_span_err!(tcx.sess, span, E0191,
"the value of the associated type `{}` (from the trait `{}`) must be specified",
name,
- tcx.item_path_str(trait_def_id));
+ tcx.item_path_str(trait_def_id))
+ .span_label(span, &format!(
+ "missing associated type `{}` value", name))
+ .emit();
}
tcx.mk_trait(object.principal, object.bounds)
}
if bounds.len() > 1 {
- let mut err = struct_span_err!(self.tcx().sess, span, E0221,
- "ambiguous associated type `{}` in bounds of `{}`",
- assoc_name,
- ty_param_name);
+ let mut err = struct_span_err!(
+ self.tcx().sess, span, E0221,
+ "ambiguous associated type `{}` in bounds of `{}`",
+ assoc_name,
+ ty_param_name);
+ err.span_label(span, &format!("ambiguous associated type `{}`", assoc_name));
for bound in &bounds {
span_note!(&mut err, span,
return self.tcx().types.err;
}
_ => {
- span_err!(tcx.sess, span, E0248,
- "found value `{}` used as a type",
- tcx.item_path_str(def.def_id()));
+ struct_span_err!(tcx.sess, span, E0248,
+ "found value `{}` used as a type",
+ tcx.item_path_str(def.def_id()))
+ .span_label(span, &format!("value used as a type"))
+ .emit();
return self.tcx().types.err;
}
}
self.check_pat(&subpat, field_ty);
}
} else {
- span_err!(tcx.sess, pat.span, E0023,
- "this pattern has {} field{s}, but the corresponding {} has {} field{s}",
- subpats.len(), def.kind_name(), variant.fields.len(),
- s = if variant.fields.len() == 1 {""} else {"s"});
+ let subpats_ending = if subpats.len() == 1 {
+ ""
+ } else {
+ "s"
+ };
+ let fields_ending = if variant.fields.len() == 1 {
+ ""
+ } else {
+ "s"
+ };
+ struct_span_err!(tcx.sess, pat.span, E0023,
+ "this pattern has {} field{}, but the corresponding {} has {} field{}",
+ subpats.len(), subpats_ending, def.kind_name(),
+ variant.fields.len(), fields_ending)
+ .span_label(pat.span, &format!("expected {} field{}, found {}",
+ variant.fields.len(), fields_ending, subpats.len()))
+ .emit();
on_error();
}
}
field_map.get(&field.name)
.map(|f| self.field_ty(span, f, substs))
.unwrap_or_else(|| {
- span_err!(tcx.sess, span, E0026,
- "struct `{}` does not have a field named `{}`",
- tcx.item_path_str(variant.did),
- field.name);
+ struct_span_err!(tcx.sess, span, E0026,
+ "struct `{}` does not have a field named `{}`",
+ tcx.item_path_str(variant.did),
+ field.name)
+ .span_label(span,
+ &format!("struct `{}` does not have field `{}`",
+ tcx.item_path_str(variant.did),
+ field.name))
+ .emit();
+
tcx.types.err
})
}
if i < type_count {
substs.types.push(space, t);
} else if i == type_count {
- span_err!(self.tcx.sess, typ.span, E0087,
- "too many type parameters provided: \
- expected at most {} parameter{}, \
- found {} parameter{}",
- type_count,
- if type_count == 1 {""} else {"s"},
- data.types.len(),
- if data.types.len() == 1 {""} else {"s"});
+ struct_span_err!(self.tcx.sess, typ.span, E0087,
+ "too many type parameters provided: \
+ expected at most {} parameter{}, \
+ found {} parameter{}",
+ type_count,
+ if type_count == 1 {""} else {"s"},
+ data.types.len(),
+ if data.types.len() == 1 {""} else {"s"})
+ .span_label(typ.span , &format!("expected {} parameter{}",
+ type_count,
+ if type_count == 1 {""} else {"s"})).emit();
substs.types.truncate(space, 0);
break;
}
}
Err(CopyImplementationError::InfrigingVariant(name)) => {
- struct_span_err!(tcx.sess, span, E0205,
- "the trait `Copy` may not be \
- implemented for this type")
- .span_label(span, &format!("variant \
- `{}` does not implement `Copy`",
- name))
- .emit()
+ let item = tcx.map.expect_item(impl_node_id);
+ let span = if let ItemImpl(_, _, _, Some(ref tr), _, _) = item.node {
+ tr.path.span
+ } else {
+ span
+ };
+
+ struct_span_err!(tcx.sess, span, E0205,
+ "the trait `Copy` may not be implemented for this type")
+ .span_label(span, &format!("variant `{}` does not implement `Copy`",
+ name))
+ .emit()
}
Err(CopyImplementationError::NotAnAdt) => {
let item = tcx.map.expect_item(impl_node_id);
match it.node {
hir::ItemFn(_,_,_,_,ref ps,_)
if ps.is_parameterized() => {
- struct_span_err!(tcx.sess, start_span, E0132,
+ let sp = if let Some(sp) = ps.span() { sp } else { start_span };
+ struct_span_err!(tcx.sess, sp, E0132,
"start function is not allowed to have type parameters")
- .span_label(ps.span().unwrap(),
+ .span_label(sp,
&format!("start function cannot have type parameters"))
.emit();
return;
/// A hash map implementation which uses linear probing with Robin
/// Hood bucket stealing.
///
-/// The hashes are all keyed by the thread-local random number generator
-/// on creation by default. This means that the ordering of the keys is
-/// randomized, but makes the tables more resistant to
-/// denial-of-service attacks (Hash DoS). No guarantees are made to the
-/// quality of the random data. The implementation uses the best available
-/// random data from your platform at the time of creation. This behavior
-/// can be overridden with one of the constructors.
+/// By default, HashMap uses a somewhat slow hashing algorithm which can provide resistance
+/// to DoS attacks. Rust makes a best attempt at acquiring random numbers without IO
+/// blocking from your system. Because of this HashMap is not guaranteed to provide
+/// DoS resistance since the numbers generated might not be truly random. If you do
+/// require this behavior you can create your own hashing function using
+/// [BuildHasherDefault](../hash/struct.BuildHasherDefault.html).
///
/// It is required that the keys implement the `Eq` and `Hash` traits, although
/// this can frequently be achieved by using `#[derive(PartialEq, Eq, Hash)]`.
Pear(u32),
}
+
fn main() {
let x = Fruit::Apple(String::new(), String::new());
match x {
Fruit::Apple(a) => {}, //~ ERROR E0023
+ //~| NOTE expected 2 fields, found 1
Fruit::Apple(a, b, c) => {}, //~ ERROR E0023
+ //~| NOTE expected 2 fields, found 3
+ Fruit::Pear(1, 2) => {}, //~ ERROR E0023
+ //~| NOTE expected 1 field, found 2
}
}
fn main() {
let thing = Thing { x: 0, y: 0 };
match thing {
- Thing { x, y, z } => {} //~ ERROR E0026
+ Thing { x, y, z } => {}
+ //~^ ERROR struct `Thing` does not have a field named `z` [E0026]
+ //~| NOTE struct `Thing` does not have field `z`
}
}
fn main() {
foo::<f64, bool>(); //~ ERROR E0087
+ //~^ NOTE expected
}
fn main() {
let irr = Irrefutable(0);
if let Irrefutable(x) = irr { //~ ERROR E0162
+ //~| NOTE irrefutable pattern
println!("{}", x);
}
}
}
type Foo = Trait; //~ ERROR E0191
+ //~| NOTE missing associated type `Bar` value
fn main() {
}
}
impl Copy for Foo { }
-//~^ ERROR E0205
+//~^ ERROR the trait `Copy` may not be implemented for this type
//~| NOTE variant `Bar` does not implement `Copy`
#[derive(Copy)]
-//~^ ERROR E0205
+//~^ ERROR the trait `Copy` may not be implemented for this type
//~| NOTE variant `Bar` does not implement `Copy`
//~| NOTE in this expansion of #[derive(Copy)]
enum Foo2<'a> {
// except according to those terms.
fn main() {
- let v: Vec(&str) = vec!["foo"]; //~ ERROR E0214
+ let v: Vec(&str) = vec!["foo"];
+ //~^ ERROR E0214
+ //~| NOTE only traits may use parentheses
}
}
fn do_something(x: Foo::Bar) { } //~ ERROR E0248
-
+ //~| NOTE value used as a type
fn main() {
}
fn dent<C:BoxCar>(c: C, color: C::Color) {
//~^ ERROR ambiguous associated type `Color` in bounds of `C`
+ //~| NOTE ambiguous associated type `Color`
//~| NOTE could derive from `Vehicle`
//~| NOTE could derive from `Box`
}
fn dent_object<COLOR>(c: BoxCar<Color=COLOR>) {
//~^ ERROR ambiguous associated type
//~| ERROR the value of the associated type `Color` (from the trait `Vehicle`) must be specified
+ //~| NOTE ambiguous associated type `Color`
//~| NOTE could derive from `Vehicle`
//~| NOTE could derive from `Box`
+ //~| NOTE missing associated type `Color` value
}
fn paint<C:BoxCar>(c: C, d: C::Color) {
//~^ ERROR ambiguous associated type `Color` in bounds of `C`
+ //~| NOTE ambiguous associated type `Color`
//~| NOTE could derive from `Vehicle`
//~| NOTE could derive from `Box`
}
--- /dev/null
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+pub fn main() {
+ let x = (0, 2);
+
+ match x {
+ (0, ref y) => {}
+ (y, 0) => {}
+ _ => (),
+ }
+}