-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add missing documentation examples for BTreeMap. #32135
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
As part of the ongoing effort to document all methods with examples, this commit adds the missing examples for the `BTreeMap` collection type. This is part of issue #29348.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,74 @@ use self::Entry::*; | |
/// It is a logic error for a key to be modified in such a way that the key's ordering relative to | ||
/// any other key, as determined by the `Ord` trait, changes while it is in the map. This is | ||
/// normally only possible through `Cell`, `RefCell`, global state, I/O, or unsafe code. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::collections::BTreeMap; | ||
/// | ||
/// // type inference lets us omit an explicit type signature (which | ||
/// // would be `BTreeMap<&str, &str>` in this example). | ||
/// let mut movie_reviews = BTreeMap::new(); | ||
/// | ||
/// // review some books. | ||
/// movie_reviews.insert("Office Space", "Deals with real issues in the workplace."); | ||
/// movie_reviews.insert("Pulp Fiction", "Masterpiece."); | ||
/// movie_reviews.insert("The Godfather", "Very enjoyable."); | ||
/// movie_reviews.insert("The Blues Brothers", "Eye lyked it alot."); | ||
/// | ||
/// // check for a specific one. | ||
/// if !movie_reviews.contains_key("Les Misérables") { | ||
/// println!("We've got {} reviews, but Les Misérables ain't one.", | ||
/// movie_reviews.len()); | ||
/// } | ||
/// | ||
/// // oops, this review has a lot of spelling mistakes, let's delete it. | ||
/// movie_reviews.remove("The Blues Brothers"); | ||
/// | ||
/// // look up the values associated with some keys. | ||
/// let to_find = ["Up!", "Office Space"]; | ||
/// for book in &to_find { | ||
/// match movie_reviews.get(book) { | ||
/// Some(review) => println!("{}: {}", book, review), | ||
/// None => println!("{} is unreviewed.", book) | ||
/// } | ||
/// } | ||
/// | ||
/// // iterate over everything. | ||
/// for (movie, review) in &movie_reviews { | ||
/// println!("{}: \"{}\"", movie, review); | ||
/// } | ||
/// ``` | ||
/// | ||
/// `BTreeMap` also implements an [`Entry API`](#method.entry), which allows | ||
/// for more complex methods of getting, setting, updating and removing keys and | ||
/// their values: | ||
/// | ||
/// ``` | ||
/// use std::collections::BTreeMap; | ||
/// | ||
/// // type inference lets us omit an explicit type signature (which | ||
/// // would be `BTreeMap<&str, u8>` in this example). | ||
/// let mut player_stats = BTreeMap::new(); | ||
/// | ||
/// fn random_stat_buff() -> u8 { | ||
/// // could actually return some random value here - let's just return | ||
/// // some fixed value for now | ||
/// 42 | ||
/// } | ||
/// | ||
/// // insert a key only if it doesn't already exist | ||
/// player_stats.entry("health").or_insert(100); | ||
/// | ||
/// // insert a key using a function that provides a new value only if it | ||
/// // doesn't already exist | ||
/// player_stats.entry("defence").or_insert_with(random_stat_buff); | ||
/// | ||
/// // update a key, guarding against the key possibly not being set | ||
/// let stat = player_stats.entry("attack").or_insert(100); | ||
/// *stat += random_stat_buff(); | ||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub struct BTreeMap<K, V> { | ||
root: node::Root<K, V>, | ||
|
@@ -276,6 +344,14 @@ pub struct OccupiedEntry<'a, K: 'a, V: 'a> { | |
|
||
impl<K: Ord, V> BTreeMap<K, V> { | ||
/// Makes a new empty BTreeMap with a reasonable choice for B. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::collections::BTreeMap; | ||
/// | ||
/// let mut map: BTreeMap<&str, isize> = BTreeMap::new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is me thinking out loud, but usually, the type of the map is inferred from usage. So we have two options:
I'm torn. maybe the second, but with a brief comment about it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually had the However, I also personally like not having the type annotation as I feel it's a bit noisy and (as you rightly say) not so realistic. I vote for 2, and have fixed accordingly! I'll try to update the other collections as well at some point for consistency. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Since I haven't gone through any of the collections yet, we can invent our own consistency, and change them all. It's okay if they're a bit inconsistent in the meantime. |
||
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn new() -> BTreeMap<K, V> { | ||
BTreeMap { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been putting a
line before the first example. Since you're going to be doing a few of these, mind putting this in? If it's too much trouble, I can also do it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a problem at all - plan to contribute a lot more going forward, so have a whole bunch of PRs to come. Best to learn the right way to do these things now!
Fixed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, I was hoping so 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I think you forgot to push commits, you said it was fixed but there's no new commit)