Skip to content

Commit c0d4abf

Browse files
committed
auto merge of #11150 : huonw/rust/moar-docs, r=alexcrichton
(I removed the `fn main` from the `std::rand` examples to make it consistent with the rest of the codebase.)
2 parents 2922697 + 8715736 commit c0d4abf

File tree

7 files changed

+189
-174
lines changed

7 files changed

+189
-174
lines changed

src/libstd/hashmap.rs

+76
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,45 @@
1212
//!
1313
//! The tables use a keyed hash with new random keys generated for each container, so the ordering
1414
//! of a set of keys in a hash table is randomized.
15+
//!
16+
//! # Example
17+
//!
18+
//! ```rust
19+
//! use std::hashmap::HashMap;
20+
//!
21+
//! // type inference lets us omit an explicit type signature (which
22+
//! // would be `HashMap<&str, &str>` in this example).
23+
//! let mut book_reviews = HashMap::new();
24+
//!
25+
//! // review some books.
26+
//! book_reviews.insert("Adventures of Hucklebury Fin", "My favorite book.");
27+
//! book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
28+
//! book_reviews.insert("Pride and Prejudice", "Very enjoyable.");
29+
//! book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
30+
//!
31+
//! // check for a specific one.
32+
//! if !book_reviews.contains_key(& &"Les Misérables") {
33+
//! println!("We've got {} reviews, but Les Misérables ain't one.",
34+
//! book_reviews.len());
35+
//! }
36+
//!
37+
//! // oops, this review has a lot of spelling mistakes, let's delete it.
38+
//! book_reviews.remove(& &"The Adventures of Sherlock Holmes");
39+
//!
40+
//! // look up the values associated with some keys.
41+
//! let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
42+
//! for book in to_find.iter() {
43+
//! match book_reviews.find(book) {
44+
//! Some(review) => println!("{}: {}", *book, *review),
45+
//! None => println!("{} is unreviewed.", *book)
46+
//! }
47+
//! }
48+
//!
49+
//! // iterate over everything.
50+
//! for (book, review) in book_reviews.iter() {
51+
//! println!("{}: \"{}\"", *book, *review);
52+
//! }
53+
//! ```
1554
1655
use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
1756
use clone::Clone;
@@ -354,6 +393,43 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
354393

355394
/// Modify and return the value corresponding to the key in the map, or
356395
/// insert and return a new value if it doesn't exist.
396+
///
397+
/// This method allows for all insertion behaviours of a hashmap,
398+
/// see methods like `insert`, `find_or_insert` and
399+
/// `insert_or_update_with` for less general and more friendly
400+
/// variations of this.
401+
///
402+
/// # Example
403+
///
404+
/// ```rust
405+
/// use std::hashmap::HashMap;
406+
///
407+
/// // map some strings to vectors of strings
408+
/// let mut map = HashMap::<~str, ~[~str]>::new();
409+
/// map.insert(~"a key", ~[~"value"]);
410+
/// map.insert(~"z key", ~[~"value"]);
411+
///
412+
/// let new = ~[~"a key", ~"b key", ~"z key"];
413+
/// for k in new.move_iter() {
414+
/// map.mangle(k, ~"new value",
415+
/// // if the key doesn't exist in the map yet, add it in
416+
/// // the obvious way.
417+
/// |_k, v| ~[v],
418+
/// // if the key does exist either prepend or append this
419+
/// // new value based on the first letter of the key.
420+
/// |key, already, new| {
421+
/// if key.starts_with("z") {
422+
/// already.unshift(new);
423+
/// } else {
424+
/// already.push(new);
425+
/// }
426+
/// });
427+
/// }
428+
///
429+
/// for (k, v) in map.iter() {
430+
/// println!("{} -> {:?}", *k, *v);
431+
/// }
432+
/// ```
357433
pub fn mangle<'a,
358434
A>(
359435
&'a mut self,

src/libstd/rand/distributions/exponential.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,9 @@ impl Rand for Exp1 {
6060
/// use std::rand;
6161
/// use std::rand::distributions::{Exp, IndependentSample};
6262
///
63-
/// fn main() {
64-
/// let exp = Exp::new(2.0);
65-
/// let v = exp.ind_sample(&mut rand::task_rng());
66-
/// println!("{} is from a Exp(2) distribution", v);
67-
/// }
63+
/// let exp = Exp::new(2.0);
64+
/// let v = exp.ind_sample(&mut rand::task_rng());
65+
/// println!("{} is from a Exp(2) distribution", v);
6866
/// ```
6967
pub struct Exp {
7068
/// `lambda` stored as `1/lambda`, since this is what we scale by.

src/libstd/rand/distributions/gamma.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@ use num;
3737
/// use std::rand;
3838
/// use std::rand::distributions::{IndependentSample, Gamma};
3939
///
40-
/// fn main() {
41-
/// let gamma = Gamma::new(2.0, 5.0);
42-
/// let v = gamma.ind_sample(&mut rand::task_rng());
43-
/// println!("{} is from a Gamma(2, 5) distribution", v);
44-
/// }
40+
/// let gamma = Gamma::new(2.0, 5.0);
41+
/// let v = gamma.ind_sample(&mut rand::task_rng());
42+
/// println!("{} is from a Gamma(2, 5) distribution", v);
4543
/// ```
4644
///
4745
/// [1]: George Marsaglia and Wai Wan Tsang. 2000. "A Simple Method
@@ -183,11 +181,9 @@ impl IndependentSample<f64> for GammaLargeShape {
183181
/// use std::rand;
184182
/// use std::rand::distributions::{ChiSquared, IndependentSample};
185183
///
186-
/// fn main() {
187-
/// let chi = ChiSquared::new(11.0);
188-
/// let v = chi.ind_sample(&mut rand::task_rng());
189-
/// println!("{} is from a χ²(11) distribution", v)
190-
/// }
184+
/// let chi = ChiSquared::new(11.0);
185+
/// let v = chi.ind_sample(&mut rand::task_rng());
186+
/// println!("{} is from a χ²(11) distribution", v)
191187
/// ```
192188
pub enum ChiSquared {
193189
// k == 1, Gamma(alpha, ..) is particularly slow for alpha < 1,
@@ -237,11 +233,9 @@ impl IndependentSample<f64> for ChiSquared {
237233
/// use std::rand;
238234
/// use std::rand::distributions::{FisherF, IndependentSample};
239235
///
240-
/// fn main() {
241-
/// let f = FisherF::new(2.0, 32.0);
242-
/// let v = f.ind_sample(&mut rand::task_rng());
243-
/// println!("{} is from an F(2, 32) distribution", v)
244-
/// }
236+
/// let f = FisherF::new(2.0, 32.0);
237+
/// let v = f.ind_sample(&mut rand::task_rng());
238+
/// println!("{} is from an F(2, 32) distribution", v)
245239
/// ```
246240
pub struct FisherF {
247241
priv numer: ChiSquared,
@@ -283,11 +277,9 @@ impl IndependentSample<f64> for FisherF {
283277
/// use std::rand;
284278
/// use std::rand::distributions::{StudentT, IndependentSample};
285279
///
286-
/// fn main() {
287-
/// let t = StudentT::new(11.0);
288-
/// let v = t.ind_sample(&mut rand::task_rng());
289-
/// println!("{} is from a t(11) distribution", v)
290-
/// }
280+
/// let t = StudentT::new(11.0);
281+
/// let v = t.ind_sample(&mut rand::task_rng());
282+
/// println!("{} is from a t(11) distribution", v)
291283
/// ```
292284
pub struct StudentT {
293285
priv chi: ChiSquared,

src/libstd/rand/distributions/mod.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,13 @@ pub struct Weighted<T> {
9494
/// use std::rand;
9595
/// use std::rand::distributions::{Weighted, WeightedChoice, IndependentSample};
9696
///
97-
/// fn main() {
98-
/// let wc = WeightedChoice::new(~[Weighted { weight: 2, item: 'a' },
99-
/// Weighted { weight: 4, item: 'b' },
100-
/// Weighted { weight: 1, item: 'c' }]);
101-
/// let mut rng = rand::task_rng();
102-
/// for _ in range(0, 16) {
103-
/// // on average prints 'a' 4 times, 'b' 8 and 'c' twice.
104-
/// println!("{}", wc.ind_sample(&mut rng));
105-
/// }
97+
/// let wc = WeightedChoice::new(~[Weighted { weight: 2, item: 'a' },
98+
/// Weighted { weight: 4, item: 'b' },
99+
/// Weighted { weight: 1, item: 'c' }]);
100+
/// let mut rng = rand::task_rng();
101+
/// for _ in range(0, 16) {
102+
/// // on average prints 'a' 4 times, 'b' 8 and 'c' twice.
103+
/// println!("{}", wc.ind_sample(&mut rng));
106104
/// }
107105
/// ```
108106
pub struct WeightedChoice<T> {

src/libstd/rand/distributions/normal.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,10 @@ impl Rand for StandardNormal {
7676
/// use std::rand;
7777
/// use std::rand::distributions::{Normal, IndependentSample};
7878
///
79-
/// fn main() {
80-
/// // mean 2, standard deviation 3
81-
/// let normal = Normal::new(2.0, 3.0);
82-
/// let v = normal.ind_sample(&mut rand::task_rng());
83-
/// println!("{} is from a N(2, 9) distribution", v)
84-
/// }
79+
/// // mean 2, standard deviation 3
80+
/// let normal = Normal::new(2.0, 3.0);
81+
/// let v = normal.ind_sample(&mut rand::task_rng());
82+
/// println!("{} is from a N(2, 9) distribution", v)
8583
/// ```
8684
pub struct Normal {
8785
priv mean: f64,
@@ -120,12 +118,10 @@ impl IndependentSample<f64> for Normal {
120118
/// use std::rand;
121119
/// use std::rand::distributions::{LogNormal, IndependentSample};
122120
///
123-
/// fn main() {
124-
/// // mean 2, standard deviation 3
125-
/// let log_normal = LogNormal::new(2.0, 3.0);
126-
/// let v = log_normal.ind_sample(&mut rand::task_rng());
127-
/// println!("{} is from an ln N(2, 9) distribution", v)
128-
/// }
121+
/// // mean 2, standard deviation 3
122+
/// let log_normal = LogNormal::new(2.0, 3.0);
123+
/// let v = log_normal.ind_sample(&mut rand::task_rng());
124+
/// println!("{} is from an ln N(2, 9) distribution", v)
129125
/// ```
130126
pub struct LogNormal {
131127
priv norm: Normal

0 commit comments

Comments
 (0)