Skip to content

Commit c2b19c1

Browse files
committed
Auto merge of #39108 - GuillaumeGomez:rollup, r=GuillaumeGomez
Rollup of 6 pull requests - Successful merges: #38247, #39028, #39065, #39084, #39105, #39106 - Failed merges:
2 parents 45b273a + 04e74ce commit c2b19c1

File tree

6 files changed

+111
-15
lines changed

6 files changed

+111
-15
lines changed

src/doc/book/patterns.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,33 @@ match x {
2323

2424
This prints `one`.
2525

26+
It's possible to create a binding for the value in the any case:
27+
28+
```rust
29+
let x = 1;
30+
31+
match x {
32+
y => println!("x: {} y: {}", x, y),
33+
}
34+
```
35+
36+
This prints:
37+
38+
```text
39+
x: 1 y: 1
40+
```
41+
42+
Note it is an error to have both a catch-all `_` and a catch-all binding in the same match block:
43+
44+
```rust
45+
let x = 1;
46+
47+
match x {
48+
y => println!("x: {} y: {}", x, y),
49+
_ => println!("anything"), // this causes an error as it is unreachable
50+
}
51+
```
52+
2653
There’s one pitfall with patterns: like anything that introduces a new binding,
2754
they introduce shadowing. For example:
2855

src/libcollections/btree/map.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
656656
}
657657

658658
/// Constructs a double-ended iterator over a sub-range of elements in the map.
659-
/// The simplest way is to use the range synax `min..max`, thus `range(min..max)` will
659+
/// The simplest way is to use the range syntax `min..max`, thus `range(min..max)` will
660660
/// yield elements from min (inclusive) to max (exclusive).
661661
/// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example
662662
/// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive
@@ -748,7 +748,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
748748
}
749749

750750
/// Constructs a mutable double-ended iterator over a sub-range of elements in the map.
751-
/// The simplest way is to use the range synax `min..max`, thus `range(min..max)` will
751+
/// The simplest way is to use the range syntax `min..max`, thus `range(min..max)` will
752752
/// yield elements from min (inclusive) to max (exclusive).
753753
/// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example
754754
/// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive

src/libcollections/btree/set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<T> BTreeSet<T> {
208208

209209
impl<T: Ord> BTreeSet<T> {
210210
/// Constructs a double-ended iterator over a sub-range of elements in the set.
211-
/// The simplest way is to use the range synax `min..max`, thus `range(min..max)` will
211+
/// The simplest way is to use the range syntax `min..max`, thus `range(min..max)` will
212212
/// yield elements from min (inclusive) to max (exclusive).
213213
/// The range may also be entered as `(Bound<T>, Bound<T>)`, so for example
214214
/// `range((Excluded(4), Included(10)))` will yield a left-exclusive, right-inclusive

src/libcore/macros.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ macro_rules! panic {
4545
/// Other use-cases of `assert!` include [testing] and enforcing run-time
4646
/// invariants in safe code (whose violation cannot result in unsafety).
4747
///
48-
/// This macro has a second version, where a custom panic message can be provided.
48+
/// This macro has a second version, where a custom panic message can
49+
/// be provided with or without arguments for formatting.
4950
///
5051
/// [testing]: ../book/testing.html
5152
///
@@ -87,12 +88,17 @@ macro_rules! assert {
8788
/// On panic, this macro will print the values of the expressions with their
8889
/// debug representations.
8990
///
91+
/// Like `assert!()`, this macro has a second version, where a custom
92+
/// panic message can be provided.
93+
///
9094
/// # Examples
9195
///
9296
/// ```
9397
/// let a = 3;
9498
/// let b = 1 + 2;
9599
/// assert_eq!(a, b);
100+
///
101+
/// assert_eq!(a, b, "we are testing addition with {} and {}", a, b);
96102
/// ```
97103
#[macro_export]
98104
#[stable(feature = "rust1", since = "1.0.0")]
@@ -125,12 +131,17 @@ macro_rules! assert_eq {
125131
/// On panic, this macro will print the values of the expressions with their
126132
/// debug representations.
127133
///
134+
/// Like `assert!()`, this macro has a second version, where a custom
135+
/// panic message can be provided.
136+
///
128137
/// # Examples
129138
///
130139
/// ```
131140
/// let a = 3;
132141
/// let b = 2;
133142
/// assert_ne!(a, b);
143+
///
144+
/// assert_ne!(a, b, "we are testing that the values are not equal");
134145
/// ```
135146
#[macro_export]
136147
#[stable(feature = "assert_ne", since = "1.12.0")]

src/libstd/env.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Inspection and manipulation of the process's environment.
1212
//!
13-
//! This module contains methods to inspect various aspects such as
13+
//! This module contains functions to inspect various aspects such as
1414
//! environment variables, process arguments, the current directory, and various
1515
//! other important directories.
1616
@@ -68,15 +68,17 @@ pub fn set_current_dir<P: AsRef<Path>>(p: P) -> io::Result<()> {
6868

6969
/// An iterator over a snapshot of the environment variables of this process.
7070
///
71-
/// This iterator is created through `std::env::vars()` and yields `(String,
72-
/// String)` pairs.
71+
/// This structure is created through the [`std::env::vars`] function.
72+
///
73+
/// [`std::env::vars`]: fn.vars.html
7374
#[stable(feature = "env", since = "1.0.0")]
7475
pub struct Vars { inner: VarsOs }
7576

7677
/// An iterator over a snapshot of the environment variables of this process.
7778
///
78-
/// This iterator is created through `std::env::vars_os()` and yields
79-
/// `(OsString, OsString)` pairs.
79+
/// This structure is created through the [`std::env::vars_os`] function.
80+
///
81+
/// [`std::env::vars_os`]: fn.vars_os.html
8082
#[stable(feature = "env", since = "1.0.0")]
8183
pub struct VarsOs { inner: os_imp::Env }
8284

@@ -218,7 +220,9 @@ fn _var_os(key: &OsStr) -> Option<OsString> {
218220
})
219221
}
220222

221-
/// Possible errors from the `env::var` method.
223+
/// Possible errors from the [`env::var`] function.
224+
///
225+
/// [env::var]: fn.var.html
222226
#[derive(Debug, PartialEq, Eq, Clone)]
223227
#[stable(feature = "env", since = "1.0.0")]
224228
pub enum VarError {
@@ -570,7 +574,7 @@ pub fn current_exe() -> io::Result<PathBuf> {
570574
/// An iterator over the arguments of a process, yielding a [`String`] value
571575
/// for each argument.
572576
///
573-
/// This structure is created through the [`std::env::args`] method.
577+
/// This structure is created through the [`std::env::args`] function.
574578
///
575579
/// [`String`]: ../string/struct.String.html
576580
/// [`std::env::args`]: ./fn.args.html
@@ -580,7 +584,7 @@ pub struct Args { inner: ArgsOs }
580584
/// An iterator over the arguments of a process, yielding an [`OsString`] value
581585
/// for each argument.
582586
///
583-
/// This structure is created through the [`std::env::args_os`] method.
587+
/// This structure is created through the [`std::env::args_os`] function.
584588
///
585589
/// [`OsString`]: ../ffi/struct.OsString.html
586590
/// [`std::env::args_os`]: ./fn.args_os.html

src/libstd/sys/unix/ext/ffi.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,38 @@ use sys_common::{FromInner, IntoInner, AsInner};
2020
/// Unix-specific extensions to `OsString`.
2121
#[stable(feature = "rust1", since = "1.0.0")]
2222
pub trait OsStringExt {
23-
/// Creates an `OsString` from a byte vector.
23+
/// Creates an [`OsString`] from a byte vector.
24+
///
25+
/// # Examples
26+
///
27+
/// ```
28+
/// use std::ffi::OsString;
29+
/// use std::os::unix::ffi::OsStringExt;
30+
///
31+
/// let bytes = b"foo".to_vec();
32+
/// let os_string = OsString::from_vec(bytes);
33+
/// assert_eq!(os_string.to_str(), Some("foo"));
34+
/// ```
35+
///
36+
/// [`OsString`]: ../../../ffi/struct.OsString.html
2437
#[stable(feature = "rust1", since = "1.0.0")]
2538
fn from_vec(vec: Vec<u8>) -> Self;
2639

27-
/// Yields the underlying byte vector of this `OsString`.
40+
/// Yields the underlying byte vector of this [`OsString`].
41+
///
42+
/// # Examples
43+
///
44+
/// ```
45+
/// use std::ffi::OsString;
46+
/// use std::os::unix::ffi::OsStringExt;
47+
///
48+
/// let mut os_string = OsString::new();
49+
/// os_string.push("foo");
50+
/// let bytes = os_string.into_vec();
51+
/// assert_eq!(bytes, b"foo");
52+
/// ```
53+
///
54+
/// [`OsString`]: ../../../ffi/struct.OsString.html
2855
#[stable(feature = "rust1", since = "1.0.0")]
2956
fn into_vec(self) -> Vec<u8>;
3057
}
@@ -43,9 +70,36 @@ impl OsStringExt for OsString {
4370
#[stable(feature = "rust1", since = "1.0.0")]
4471
pub trait OsStrExt {
4572
#[stable(feature = "rust1", since = "1.0.0")]
73+
/// Creates an [`OsStr`] from a byte slice.
74+
///
75+
/// # Examples
76+
///
77+
/// ```
78+
/// use std::ffi::OsStr;
79+
/// use std::os::unix::ffi::OsStrExt;
80+
///
81+
/// let bytes = b"foo";
82+
/// let os_str = OsStr::from_bytes(bytes);
83+
/// assert_eq!(os_str.to_str(), Some("foo"));
84+
/// ```
85+
///
86+
/// [`OsStr`]: ../../../ffi/struct.OsStr.html
4687
fn from_bytes(slice: &[u8]) -> &Self;
4788

48-
/// Gets the underlying byte view of the `OsStr` slice.
89+
/// Gets the underlying byte view of the [`OsStr`] slice.
90+
///
91+
/// # Examples
92+
///
93+
/// ```
94+
/// use std::ffi::OsStr;
95+
/// use std::os::unix::ffi::OsStrExt;
96+
///
97+
/// let mut os_str = OsStr::new("foo");
98+
/// let bytes = os_str.as_bytes();
99+
/// assert_eq!(bytes, b"foo");
100+
/// ```
101+
///
102+
/// [`OsStr`]: ../../../ffi/struct.OsStr.html
49103
#[stable(feature = "rust1", since = "1.0.0")]
50104
fn as_bytes(&self) -> &[u8];
51105
}

0 commit comments

Comments
 (0)