October 2018
Beginner
180 pages
4h 48m
English
Within an implementation block that has generic type parameters, we can use those parameter names as part of function return values, too.
Here is an example function that uses generic type parameter names in its return data type:
fn get_ref(&self, key: K) -> Result<&V, String> { if key == self.key { return Ok(&self.value); } else if key < self.key { match self.lesser { None => { return Err("No such key".to_string()); } Some(ref lesser) => { return lesser.get_ref(key); } } } else { match self.greater { None => { return Err("No such key".to_string()); } Some(ref greater) => { return greater.get_ref(key); } } } }
This function looks up a key in the binary tree, and returns an immutable borrow of ...
Read now
Unlock full access