Skip to content

Commit 826b835

Browse files
committed
auto merge of rust-lang#15749 : vhbit/rust/treemap-doc-fixes, r=alexcrichton
1. Removed obsolete comment regarding recursive/iteration implementations of tree_find_with/tree_find_mut_with 2. Replaced easy breakable find_with example with simpler one (which only removes redundant allocation during search)
2 parents b10dcbe + 4a00d4e commit 826b835

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/libcollections/treemap.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -190,33 +190,38 @@ impl<K: Ord, V> TreeMap<K, V> {
190190
}
191191

192192
impl<K, V> TreeMap<K, V> {
193-
/// Return the value for which f(key) returns Equal. f is invoked
194-
/// with current key and helps to navigate the tree
193+
/// Return the value for which `f(key)` returns `Equal`. `f` is invoked
194+
/// with current key and guides tree navigation. That means `f` should
195+
/// be aware of natural ordering of the tree.
195196
///
196197
/// # Example
197198
///
198199
/// ```
199-
/// use std::ascii::StrAsciiExt;
200+
/// use collections::treemap::TreeMap;
200201
///
201-
/// let mut t = collections::treemap::TreeMap::new();
202-
/// t.insert("Content-Type", "application/xml");
203-
/// t.insert("User-Agent", "Curl-Rust/0.1");
202+
/// fn get_headers() -> TreeMap<String, String> {
203+
/// let mut result = TreeMap::new();
204+
/// result.insert("Content-Type".to_string(), "application/xml".to_string());
205+
/// result.insert("User-Agent".to_string(), "Curl-Rust/0.1".to_string());
206+
/// result
207+
/// }
204208
///
205-
/// let ua_key = "user-agent";
206-
/// let ua = t.find_with(|&k| {
207-
/// ua_key.cmp(&k.to_ascii_lower().as_slice())
209+
/// let headers = get_headers();
210+
/// let ua_key = "User-Agent";
211+
/// let ua = headers.find_with(|k| {
212+
/// ua_key.cmp(&k.as_slice())
208213
/// });
209214
///
210-
/// assert_eq!(*ua.unwrap(), "Curl-Rust/0.1");
215+
/// assert_eq!((*ua.unwrap()).as_slice(), "Curl-Rust/0.1");
211216
/// ```
212217
#[inline]
213218
pub fn find_with<'a>(&'a self, f:|&K| -> Ordering) -> Option<&'a V> {
214219
tree_find_with(&self.root, f)
215220
}
216221

217-
/// Return the value for which f(key) returns Equal. f is invoked
218-
/// with current key and helps to navigate the tree
219-
///
222+
/// Return the value for which `f(key)` returns `Equal`. `f` is invoked
223+
/// with current key and guides tree navigation. That means `f` should
224+
/// be aware of natural ordering of the tree.
220225
/// # Example
221226
///
222227
/// ```
@@ -913,14 +918,9 @@ fn split<K: Ord, V>(node: &mut Box<TreeNode<K, V>>) {
913918
}
914919
}
915920

916-
// Next 2 functions have the same conventions
917-
//
918-
// The only difference is that non-mutable version uses loop instead
919-
// of recursion (performance considerations)
920-
// It seems to be impossible to avoid recursion with mutability
921-
//
922-
// So convention is that comparator is gets at input current key
923-
// and returns search_key cmp cur_key (i.e. search_key.cmp(cur_key))
921+
// Next 2 functions have the same convention: comparator gets
922+
// at input current key and returns search_key cmp cur_key
923+
// (i.e. search_key.cmp(&cur_key))
924924
fn tree_find_with<'r, K, V>(node: &'r Option<Box<TreeNode<K, V>>>,
925925
f: |&K| -> Ordering) -> Option<&'r V> {
926926
let mut current: &'r Option<Box<TreeNode<K, V>>> = node;

0 commit comments

Comments
 (0)