Skip to content

Rollup of 7 pull requests #34939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
42326ec
Add examples for VecDeque
GuillaumeGomez Jul 16, 2016
0c9a6f6
Add examples for LinkedList
GuillaumeGomez Jul 16, 2016
3b5d71e
Make .enumerate() example self-explanatory
Jul 17, 2016
6a09df9
implement AddAssign for String
oconnor663 Jul 17, 2016
ce442b4
Add debug for hash_map::{Entry, VacantEntry, OccupiedEntry}
GuillaumeGomez Jul 20, 2016
d820fcb
Remove rustdoc reference to `walk_dir`
mark-buer Jul 18, 2016
cac58ab
update the since field to 1.12.0 for String AddAssign
oconnor663 Jul 18, 2016
9b81306
use a new feature name for String AddAssign
oconnor663 Jul 18, 2016
f77bcc8
rustc: Remove soft-float from MIPS targets
alexcrichton Jul 18, 2016
4a2116b
[CSS] Fix unwanted top margin for toggle wrapper
GuillaumeGomez Jul 19, 2016
a005b2c
Rewrite/expand doc examples for `Vec::set_len`.
frewsxcv Jul 19, 2016
00e3149
Add doc examples for `Vec::{as_slice,as_mut_slice}`.
frewsxcv Jul 20, 2016
9b5db22
Add doc for btree_map types
GuillaumeGomez Jul 19, 2016
fbfee42
core: impl From<T> for Option<T>
seanmonstar Jul 14, 2016
bcbe27c
Rollup merge of #34828 - seanmonstar:into-opton, r=alexcrichton
GuillaumeGomez Jul 21, 2016
a168e30
Rollup merge of #34854 - GuillaumeGomez:linked_list_doc, r=steveklabnik
GuillaumeGomez Jul 21, 2016
705d92d
Rollup merge of #34855 - GuillaumeGomez:vec_deque_doc, r=steveklabnik
GuillaumeGomez Jul 21, 2016
9ba1792
Rollup merge of #34880 - xitep:master, r=steveklabnik
GuillaumeGomez Jul 21, 2016
4817c5e
Rollup merge of #34890 - oconnor663:addassign, r=brson
GuillaumeGomez Jul 21, 2016
271230f
Rollup merge of #34895 - mark-buer:patch-1, r=steveklabnik
GuillaumeGomez Jul 21, 2016
91fd838
Rollup merge of #34910 - alexcrichton:hard-float-mips, r=brson
GuillaumeGomez Jul 21, 2016
27876c0
Rollup merge of #34911 - frewsxcv:vec-set-len, r=steveklabnik
GuillaumeGomez Jul 21, 2016
d62f8dd
Rollup merge of #34919 - GuillaumeGomez:btree_map_doc, r=steveklabnik
GuillaumeGomez Jul 21, 2016
3f3dabb
Rollup merge of #34921 - GuillaumeGomez:css_fix, r=steveklabnik
GuillaumeGomez Jul 21, 2016
1006f79
Rollup merge of #34930 - frewsxcv:vec-as-slice, r=steveklabnik
GuillaumeGomez Jul 21, 2016
22a14a8
Rollup merge of #34937 - GuillaumeGomez:hash_map_entry_debug, r=apase…
GuillaumeGomez Jul 21, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/doc/book/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,19 @@ When you need to keep track of how many times you already looped, you can use th
#### On ranges:

```rust
for (i, j) in (5..10).enumerate() {
println!("i = {} and j = {}", i, j);
for (index, value) in (5..10).enumerate() {
println!("index = {} and value = {}", index, value);
}
```

Outputs:

```text
i = 0 and j = 5
i = 1 and j = 6
i = 2 and j = 7
i = 3 and j = 8
i = 4 and j = 9
index = 0 and value = 5
index = 1 and value = 6
index = 2 and value = 7
index = 3 and value = 8
index = 4 and value = 9
```

Don't forget to add the parentheses around the range.
Expand Down
191 changes: 189 additions & 2 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ pub struct RangeMut<'a, K: 'a, V: 'a> {
}

/// A view into a single entry in a map, which may either be vacant or occupied.
/// This enum is constructed from the [`entry`] method on [`BTreeMap`].
///
/// [`BTreeMap`]: struct.BTreeMap.html
/// [`entry`]: struct.BTreeMap.html#method.entry
#[stable(feature = "rust1", since = "1.0.0")]
pub enum Entry<'a, K: 'a, V: 'a> {
/// A vacant Entry
Expand Down Expand Up @@ -340,7 +344,9 @@ impl<'a, K: 'a + Debug + Ord, V: 'a + Debug> Debug for Entry<'a, K, V> {
}
}

/// A vacant Entry.
/// A vacant Entry. It is part of the [`Entry`] enum.
///
/// [`Entry`]: enum.Entry.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct VacantEntry<'a, K: 'a, V: 'a> {
key: K,
Expand All @@ -360,7 +366,9 @@ impl<'a, K: 'a + Debug + Ord, V: 'a> Debug for VacantEntry<'a, K, V> {
}
}

/// An occupied Entry.
/// An occupied Entry. It is part of the [`Entry`] enum.
///
/// [`Entry`]: enum.Entry.html
#[stable(feature = "rust1", since = "1.0.0")]
pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
handle: Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, marker::KV>,
Expand Down Expand Up @@ -1890,6 +1898,17 @@ impl<K, V> BTreeMap<K, V> {
impl<'a, K: Ord, V> Entry<'a, K, V> {
/// Ensures a value is in the entry by inserting the default if empty, and returns
/// a mutable reference to the value in the entry.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// map.entry("poneyland").or_insert(12);
///
/// assert_eq!(map["poneyland"], 12);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Expand All @@ -1900,6 +1919,19 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {

/// Ensures a value is in the entry by inserting the result of the default function if empty,
/// and returns a mutable reference to the value in the entry.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map: BTreeMap<&str, String> = BTreeMap::new();
/// let s = "hoho".to_owned();
///
/// map.entry("poneyland").or_insert_with(|| s);
///
/// assert_eq!(map["poneyland"], "hoho".to_owned());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Expand All @@ -1909,6 +1941,15 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
}

/// Returns a reference to this entry's key.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// assert_eq!(map.entry("poneyland").key(), &"poneyland");
/// ```
#[stable(feature = "map_entry_keys", since = "1.10.0")]
pub fn key(&self) -> &K {
match *self {
Expand All @@ -1921,19 +1962,58 @@ impl<'a, K: Ord, V> Entry<'a, K, V> {
impl<'a, K: Ord, V> VacantEntry<'a, K, V> {
/// Gets a reference to the key that would be used when inserting a value
/// through the VacantEntry.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// assert_eq!(map.entry("poneyland").key(), &"poneyland");
/// ```
#[stable(feature = "map_entry_keys", since = "1.10.0")]
pub fn key(&self) -> &K {
&self.key
}

/// Take ownership of the key.
///
/// # Examples
///
/// ```
/// #![feature(map_entry_recover_keys)]
///
/// use std::collections::BTreeMap;
/// use std::collections::btree_map::Entry;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
///
/// if let Entry::Vacant(v) = map.entry("poneyland") {
/// v.into_key();
/// }
/// ```
#[unstable(feature = "map_entry_recover_keys", issue = "34285")]
pub fn into_key(self) -> K {
self.key
}

/// Sets the value of the entry with the VacantEntry's key,
/// and returns a mutable reference to it.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut count: BTreeMap<&str, usize> = BTreeMap::new();
///
/// // count the number of occurrences of letters in the vec
/// for x in vec!["a","b","a","c","a","b"] {
/// *count.entry(x).or_insert(0) += 1;
/// }
///
/// assert_eq!(count["a"], 3);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(self, value: V) -> &'a mut V {
*self.length += 1;
Expand Down Expand Up @@ -1979,43 +2059,150 @@ impl<'a, K: Ord, V> VacantEntry<'a, K, V> {

impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
/// Gets a reference to the key in the entry.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// map.entry("poneyland").or_insert(12);
/// assert_eq!(map.entry("poneyland").key(), &"poneyland");
/// ```
#[stable(feature = "map_entry_keys", since = "1.10.0")]
pub fn key(&self) -> &K {
self.handle.reborrow().into_kv().0
}

/// Take ownership of the key and value from the map.
///
/// # Examples
///
/// ```
/// #![feature(map_entry_recover_keys)]
///
/// use std::collections::BTreeMap;
/// use std::collections::btree_map::Entry;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// map.entry("poneyland").or_insert(12);
///
/// if let Entry::Occupied(o) = map.entry("poneyland") {
/// // We delete the entry from the map.
/// o.remove_pair();
/// }
///
/// // If now try to get the value, it will panic:
/// // println!("{}", map["poneyland"]);
/// ```
#[unstable(feature = "map_entry_recover_keys", issue = "34285")]
pub fn remove_pair(self) -> (K, V) {
self.remove_kv()
}

/// Gets a reference to the value in the entry.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use std::collections::btree_map::Entry;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// map.entry("poneyland").or_insert(12);
///
/// if let Entry::Occupied(o) = map.entry("poneyland") {
/// assert_eq!(o.get(), &12);
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get(&self) -> &V {
self.handle.reborrow().into_kv().1
}

/// Gets a mutable reference to the value in the entry.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use std::collections::btree_map::Entry;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// map.entry("poneyland").or_insert(12);
///
/// assert_eq!(map["poneyland"], 12);
/// if let Entry::Occupied(mut o) = map.entry("poneyland") {
/// *o.get_mut() += 10;
/// }
/// assert_eq!(map["poneyland"], 22);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn get_mut(&mut self) -> &mut V {
self.handle.kv_mut().1
}

/// Converts the entry into a mutable reference to its value.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use std::collections::btree_map::Entry;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// map.entry("poneyland").or_insert(12);
///
/// assert_eq!(map["poneyland"], 12);
/// if let Entry::Occupied(o) = map.entry("poneyland") {
/// *o.into_mut() += 10;
/// }
/// assert_eq!(map["poneyland"], 22);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn into_mut(self) -> &'a mut V {
self.handle.into_kv_mut().1
}

/// Sets the value of the entry with the OccupiedEntry's key,
/// and returns the entry's old value.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use std::collections::btree_map::Entry;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// map.entry("poneyland").or_insert(12);
///
/// if let Entry::Occupied(mut o) = map.entry("poneyland") {
/// assert_eq!(o.insert(15), 12);
/// }
/// assert_eq!(map["poneyland"], 15);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn insert(&mut self, value: V) -> V {
mem::replace(self.get_mut(), value)
}

/// Takes the value of the entry out of the map, and returns it.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeMap;
/// use std::collections::btree_map::Entry;
///
/// let mut map: BTreeMap<&str, usize> = BTreeMap::new();
/// map.entry("poneyland").or_insert(12);
///
/// if let Entry::Occupied(o) = map.entry("poneyland") {
/// assert_eq!(o.remove(), 12);
/// }
/// // If we try to get "poneyland"'s value, it'll panic:
/// // println!("{}", map["poneyland"]);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn remove(self) -> V {
self.remove_kv().1
Expand Down
Loading