diff options
| author | Bruno de Oliveira Abinader <bruno.d@partner.samsung.com> | 2014-02-24 22:54:34 -0400 |
|---|---|---|
| committer | Bruno de Oliveira Abinader <bruno.d@partner.samsung.com> | 2014-02-27 08:35:46 -0400 |
| commit | e589fcffcc8da46b5949d15284a466d9ed27a003 (patch) | |
| tree | 46eb9a0648af8123d64fc6351025449abdaf6734 /src | |
| parent | 197116d7ce7862b21eac6af498a2726d7a1bc3d6 (diff) | |
| download | rust-e589fcffcc8da46b5949d15284a466d9ed27a003.tar.gz rust-e589fcffcc8da46b5949d15284a466d9ed27a003.zip | |
Implemented list::len() based on Container trait
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcollections/list.rs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index ddac516d8bb..9223669c787 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -10,6 +10,8 @@ //! A standard, garbage-collected linked list. +use std::container::Container; + #[deriving(Clone, Eq)] #[allow(missing_doc)] pub enum List<T> { @@ -53,6 +55,11 @@ impl<T> List<T> { } } +impl<T> Container for List<T> { + /// Returns the length of a list + fn len(&self) -> uint { self.iter().len() } +} + /// Returns true if a list contains an element with the given value pub fn has<T:Eq>(list: @List<T>, element: T) -> bool { let mut found = false; @@ -70,13 +77,6 @@ pub fn is_empty<T>(list: @List<T>) -> bool { } } -/// Returns the length of a list -pub fn len<T>(list: @List<T>) -> uint { - let mut count = 0u; - iter(list, |_e| count += 1u); - count -} - /// Returns all but the first element of a list pub fn tail<T>(list: @List<T>) -> @List<T> { match *list { @@ -252,10 +252,11 @@ mod tests { #[test] fn test_len() { - let list = @List::from_vec([0, 1, 2]); - let empty = @list::Nil::<int>; - assert_eq!(list::len(list), 3u); - assert_eq!(list::len(empty), 0u); + let empty = Nil::<int>; + assert_eq!(empty.len(), 0u); + + let list = List::from_vec([0, 1, 2]); + assert_eq!(list.len(), 3u); } #[test] |
