Skip to content

Commit d0da7f6

Browse files

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+639
-139
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ To find documentation-related issues, sort by the [A-docs label][adocs].
230230
In many cases, you don't need a full `make doc`. You can use `rustdoc` directly
231231
to check small fixes. For example, `rustdoc src/doc/reference.md` will render
232232
reference to `doc/reference.html`. The CSS might be messed up, but you can
233-
verify that HTML is right.
233+
verify that the HTML is right.
234234

235235
## Issue Triage
236236

src/doc/book/traits.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ As you can see, the `trait` block looks very similar to the `impl` block,
4747
but we don’t define a body, only a type signature. When we `impl` a trait,
4848
we use `impl Trait for Item`, rather than only `impl Item`.
4949

50+
`Self` may be used in a type annotation to refer to an instance of the type
51+
implementing this trait passed as a parameter. `Self`, `&Self` or `&mut Self`
52+
may be used depending on the level of ownership required.
53+
54+
```rust
55+
struct Circle {
56+
x: f64,
57+
y: f64,
58+
radius: f64,
59+
}
60+
61+
trait HasArea {
62+
fn area(&self) -> f64;
63+
64+
fn is_larger(&self, &Self) -> bool;
65+
}
66+
67+
impl HasArea for Circle {
68+
fn area(&self) -> f64 {
69+
std::f64::consts::PI * (self.radius * self.radius)
70+
}
71+
72+
fn is_larger(&self, other: &Self) -> bool {
73+
self.area() > other.area()
74+
}
75+
}
76+
```
77+
5078
## Trait bounds on generic functions
5179

5280
Traits are useful because they allow a type to make certain promises about its

src/etc/CONFIGS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ These are some links to repos with configs which ease the use of rust.
66

77
* [rust.vim](https://github.com/rust-lang/rust.vim)
88
* [emacs rust-mode](https://github.com/rust-lang/rust-mode)
9+
* [sublime-rust](https://github.com/rust-lang/sublime-rust)
910
* [gedit-config](https://github.com/rust-lang/gedit-config)
1011
* [kate-config](https://github.com/rust-lang/kate-config)
1112
* [nano-config](https://github.com/rust-lang/nano-config)

src/liballoc/rc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ impl<T> Rc<T> {
263263
}
264264

265265
/// Checks if `Rc::try_unwrap` would return `Ok`.
266+
///
267+
/// # Examples
268+
///
269+
/// ```
270+
/// #![feature(rc_would_unwrap)]
271+
///
272+
/// use std::rc::Rc;
273+
///
274+
/// let x = Rc::new(3);
275+
/// assert!(Rc::would_unwrap(&x));
276+
/// assert_eq!(Rc::try_unwrap(x), Ok(3));
277+
///
278+
/// let x = Rc::new(4);
279+
/// let _y = x.clone();
280+
/// assert!(!Rc::would_unwrap(&x));
281+
/// assert_eq!(Rc::try_unwrap(x), Err(Rc::new(4)));
282+
/// ```
266283
#[unstable(feature = "rc_would_unwrap",
267284
reason = "just added for niche usecase",
268285
issue = "28356")]

src/libcollections/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ use boxed::Box;
132132
/// [`OsString`]: ../../std/ffi/struct.OsString.html
133133
///
134134
/// Indexing is intended to be a constant-time operation, but UTF-8 encoding
135-
/// does not allow us to do this. Furtheremore, it's not clear what sort of
135+
/// does not allow us to do this. Furthermore, it's not clear what sort of
136136
/// thing the index should return: a byte, a codepoint, or a grapheme cluster.
137137
/// The [`as_bytes()`] and [`chars()`] methods return iterators over the first
138138
/// two, respectively.

src/libcore/ops.rs

Lines changed: 122 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -299,26 +299,63 @@ sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
299299
///
300300
/// # Examples
301301
///
302-
/// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
303-
/// calling `mul`, and therefore, `main` prints `Multiplying!`.
302+
/// Implementing a `Mul`tipliable rational number struct:
304303
///
305304
/// ```
306305
/// use std::ops::Mul;
307306
///
308-
/// struct Foo;
307+
/// // The uniqueness of rational numbers in lowest terms is a consequence of
308+
/// // the fundamental theorem of arithmetic.
309+
/// #[derive(Eq)]
310+
/// #[derive(PartialEq, Debug)]
311+
/// struct Rational {
312+
/// nominator: usize,
313+
/// denominator: usize,
314+
/// }
309315
///
310-
/// impl Mul for Foo {
311-
/// type Output = Foo;
316+
/// impl Rational {
317+
/// fn new(nominator: usize, denominator: usize) -> Self {
318+
/// if denominator == 0 {
319+
/// panic!("Zero is an invalid denominator!");
320+
/// }
312321
///
313-
/// fn mul(self, _rhs: Foo) -> Foo {
314-
/// println!("Multiplying!");
315-
/// self
322+
/// // Reduce to lowest terms by dividing by the greatest common
323+
/// // divisor.
324+
/// let gcd = gcd(nominator, denominator);
325+
/// Rational {
326+
/// nominator: nominator / gcd,
327+
/// denominator: denominator / gcd,
328+
/// }
316329
/// }
317330
/// }
318331
///
319-
/// fn main() {
320-
/// Foo * Foo;
332+
/// impl Mul for Rational {
333+
/// // The multiplication of rational numbers is a closed operation.
334+
/// type Output = Self;
335+
///
336+
/// fn mul(self, rhs: Self) -> Self {
337+
/// let nominator = self.nominator * rhs.nominator;
338+
/// let denominator = self.denominator * rhs.denominator;
339+
/// Rational::new(nominator, denominator)
340+
/// }
341+
/// }
342+
///
343+
/// // Euclid's two-thousand-year-old algorithm for finding the greatest common
344+
/// // divisor.
345+
/// fn gcd(x: usize, y: usize) -> usize {
346+
/// let mut x = x;
347+
/// let mut y = y;
348+
/// while y != 0 {
349+
/// let t = y;
350+
/// y = x % y;
351+
/// x = t;
352+
/// }
353+
/// x
321354
/// }
355+
///
356+
/// assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
357+
/// assert_eq!(Rational::new(2, 3) * Rational::new(3, 4),
358+
/// Rational::new(1, 2));
322359
/// ```
323360
///
324361
/// Note that `RHS = Self` by default, but this is not mandatory. Here is an
@@ -486,26 +523,34 @@ div_impl_float! { f32 f64 }
486523
///
487524
/// # Examples
488525
///
489-
/// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
490-
/// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
526+
/// This example implements `Rem` on a `SplitSlice` object. After `Rem` is
527+
/// implemented, one can use the `%` operator to find out what the remaining
528+
/// elements of the slice would be after splitting it into equal slices of a
529+
/// given length.
491530
///
492531
/// ```
493532
/// use std::ops::Rem;
494533
///
495-
/// struct Foo;
534+
/// #[derive(PartialEq, Debug)]
535+
/// struct SplitSlice<'a, T: 'a> {
536+
/// slice: &'a [T],
537+
/// }
496538
///
497-
/// impl Rem for Foo {
498-
/// type Output = Foo;
539+
/// impl<'a, T> Rem<usize> for SplitSlice<'a, T> {
540+
/// type Output = SplitSlice<'a, T>;
499541
///
500-
/// fn rem(self, _rhs: Foo) -> Foo {
501-
/// println!("Remainder-ing!");
502-
/// self
542+
/// fn rem(self, modulus: usize) -> Self {
543+
/// let len = self.slice.len();
544+
/// let rem = len % modulus;
545+
/// let start = len - rem;
546+
/// SplitSlice {slice: &self.slice[start..]}
503547
/// }
504548
/// }
505549
///
506-
/// fn main() {
507-
/// Foo % Foo;
508-
/// }
550+
/// // If we were to divide &[0, 1, 2, 3, 4, 5, 6, 7] into slices of size 3,
551+
/// // the remainder would be &[6, 7]
552+
/// assert_eq!(SplitSlice { slice: &[0, 1, 2, 3, 4, 5, 6, 7] } % 3,
553+
/// SplitSlice { slice: &[6, 7] });
509554
/// ```
510555
#[lang = "rem"]
511556
#[stable(feature = "rust1", since = "1.0.0")]
@@ -694,26 +739,41 @@ not_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
694739
///
695740
/// # Examples
696741
///
697-
/// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
698-
/// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
742+
/// In this example, the `BitAnd` trait is implemented for a `BooleanVector`
743+
/// struct.
699744
///
700745
/// ```
701746
/// use std::ops::BitAnd;
702747
///
703-
/// struct Foo;
704-
///
705-
/// impl BitAnd for Foo {
706-
/// type Output = Foo;
707-
///
708-
/// fn bitand(self, _rhs: Foo) -> Foo {
709-
/// println!("Bitwise And-ing!");
710-
/// self
748+
/// #[derive(Debug)]
749+
/// struct BooleanVector {
750+
/// value: Vec<bool>,
751+
/// };
752+
///
753+
/// impl BitAnd for BooleanVector {
754+
/// type Output = Self;
755+
///
756+
/// fn bitand(self, rhs: Self) -> Self {
757+
/// BooleanVector {
758+
/// value: self.value
759+
/// .iter()
760+
/// .zip(rhs.value.iter())
761+
/// .map(|(x, y)| *x && *y)
762+
/// .collect(),
763+
/// }
711764
/// }
712765
/// }
713766
///
714-
/// fn main() {
715-
/// Foo & Foo;
767+
/// impl PartialEq for BooleanVector {
768+
/// fn eq(&self, other: &Self) -> bool {
769+
/// self.value == other.value
770+
/// }
716771
/// }
772+
///
773+
/// let bv1 = BooleanVector { value: vec![true, true, false, false] };
774+
/// let bv2 = BooleanVector { value: vec![true, false, true, false] };
775+
/// let expected = BooleanVector { value: vec![true, false, false, false] };
776+
/// assert_eq!(bv1 & bv2, expected);
717777
/// ```
718778
#[lang = "bitand"]
719779
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1490,28 +1550,44 @@ shr_assign_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
14901550
///
14911551
/// # Examples
14921552
///
1493-
/// A trivial implementation of `Index`. When `Foo[Bar]` happens, it ends up
1494-
/// calling `index`, and therefore, `main` prints `Indexing!`.
1553+
/// This example implements `Index` on a read-only `NucleotideCount` container,
1554+
/// enabling individual counts to be retrieved with index syntax.
14951555
///
14961556
/// ```
14971557
/// use std::ops::Index;
14981558
///
1499-
/// #[derive(Copy, Clone)]
1500-
/// struct Foo;
1501-
/// struct Bar;
1559+
/// enum Nucleotide {
1560+
/// A,
1561+
/// C,
1562+
/// G,
1563+
/// T,
1564+
/// }
15021565
///
1503-
/// impl Index<Bar> for Foo {
1504-
/// type Output = Foo;
1566+
/// struct NucleotideCount {
1567+
/// a: usize,
1568+
/// c: usize,
1569+
/// g: usize,
1570+
/// t: usize,
1571+
/// }
15051572
///
1506-
/// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
1507-
/// println!("Indexing!");
1508-
/// self
1573+
/// impl Index<Nucleotide> for NucleotideCount {
1574+
/// type Output = usize;
1575+
///
1576+
/// fn index(&self, nucleotide: Nucleotide) -> &usize {
1577+
/// match nucleotide {
1578+
/// Nucleotide::A => &self.a,
1579+
/// Nucleotide::C => &self.c,
1580+
/// Nucleotide::G => &self.g,
1581+
/// Nucleotide::T => &self.t,
1582+
/// }
15091583
/// }
15101584
/// }
15111585
///
1512-
/// fn main() {
1513-
/// Foo[Bar];
1514-
/// }
1586+
/// let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
1587+
/// assert_eq!(nucleotide_count[Nucleotide::A], 14);
1588+
/// assert_eq!(nucleotide_count[Nucleotide::C], 9);
1589+
/// assert_eq!(nucleotide_count[Nucleotide::G], 10);
1590+
/// assert_eq!(nucleotide_count[Nucleotide::T], 12);
15151591
/// ```
15161592
#[lang = "index"]
15171593
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]

src/libcore/ptr.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ pub unsafe fn replace<T>(dest: *mut T, mut src: T) -> T {
128128
/// let x = 12;
129129
/// let y = &x as *const i32;
130130
///
131-
/// unsafe { println!("{}", std::ptr::read(y)); }
131+
/// unsafe {
132+
/// assert_eq!(std::ptr::read(y), 12);
133+
/// }
132134
/// ```
133135
#[inline(always)]
134136
#[stable(feature = "rust1", since = "1.0.0")]
@@ -178,7 +180,7 @@ pub unsafe fn read_and_drop<T>(dest: *mut T) -> T {
178180
///
179181
/// unsafe {
180182
/// std::ptr::write(y, z);
181-
/// println!("{}", std::ptr::read(y));
183+
/// assert_eq!(std::ptr::read(y), 12);
182184
/// }
183185
/// ```
184186
#[inline]
@@ -220,7 +222,9 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
220222
/// let x = 12;
221223
/// let y = &x as *const i32;
222224
///
223-
/// unsafe { println!("{}", std::ptr::read_volatile(y)); }
225+
/// unsafe {
226+
/// assert_eq!(std::ptr::read_volatile(y), 12);
227+
/// }
224228
/// ```
225229
#[inline]
226230
#[stable(feature = "volatile", since = "1.9.0")]
@@ -266,7 +270,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
266270
///
267271
/// unsafe {
268272
/// std::ptr::write_volatile(y, z);
269-
/// println!("{}", std::ptr::read_volatile(y));
273+
/// assert_eq!(std::ptr::read_volatile(y), 12);
270274
/// }
271275
/// ```
272276
#[inline]

src/librustc/middle/privacy.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ use syntax::ast::NodeId;
2323
pub enum AccessLevel {
2424
// Exported items + items participating in various kinds of public interfaces,
2525
// but not directly nameable. For example, if function `fn f() -> T {...}` is
26-
// public, then type `T` is exported. Its values can be obtained by other crates
27-
// even if the type itseld is not nameable.
28-
// FIXME: Mostly unimplemented. Only `type` aliases export items currently.
26+
// public, then type `T` is reachable. Its values can be obtained by other crates
27+
// even if the type itself is not nameable.
2928
Reachable,
3029
// Public items + items accessible to other crates with help of `pub use` reexports
3130
Exported,

src/librustc_borrowck/borrowck/check_loans.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,10 +647,13 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
647647
struct_span_err!(self.bccx, span, E0503,
648648
"cannot use `{}` because it was mutably borrowed",
649649
&self.bccx.loan_path_to_string(copy_path))
650-
.span_note(loan_span,
650+
.span_label(loan_span,
651651
&format!("borrow of `{}` occurs here",
652652
&self.bccx.loan_path_to_string(&loan_path))
653653
)
654+
.span_label(span,
655+
&format!("use of borrowed `{}`",
656+
&self.bccx.loan_path_to_string(&loan_path)))
654657
.emit();
655658
}
656659
}

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,11 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
914914
}
915915
mc::AliasableStatic |
916916
mc::AliasableStaticMut => {
917-
struct_span_err!(
917+
let mut err = struct_span_err!(
918918
self.tcx.sess, span, E0388,
919-
"{} in a static location", prefix)
919+
"{} in a static location", prefix);
920+
err.span_label(span, &format!("cannot write data in a static definition"));
921+
err
920922
}
921923
mc::AliasableBorrowed => {
922924
struct_span_err!(

0 commit comments

Comments
 (0)