-
-
Save rust-play/06bc373e7a7495653c1f3663bd0519f6 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Implementing Vector | |
pub struct ToyVec<T> { | |
elements: Box<[T]>, | |
len: usize, | |
} | |
// Set Trait boundary of Default to get the default value of T | |
impl<T: Default> ToyVec<T> { | |
pub fn new() -> Self { | |
Self::with_capacity(2) | |
} | |
pub fn with_capacity(capacity: usize) -> Self { | |
Self { | |
elements: Self::allocate_in_heap(capacity), | |
len: 0 | |
} | |
} | |
pub fn len(&self) -> usize { | |
self.len | |
} | |
pub fn capacity(&self) -> usize { | |
self.elements.len() | |
} | |
// Passing mutable reference of self, so we are manipulating ToyVec | |
// Passing T type, meaning the ownership of element will move to this function then to ToyVec | |
pub fn push(&mut self, element: T) { | |
if self.len == self.capacity() { | |
self.grow(); | |
} | |
self.elements[self.len] = element; | |
self.len += 1; | |
} | |
// Passing reference without mutable, so we are not manipulating ToyVec | |
// Function returns immutable reference which self owns or none | |
pub fn get(&self, index: usize) -> Option<&T> { | |
if index < self.len { | |
Some(&self.elements[index]) | |
} else { | |
None | |
} | |
} | |
// This will show a compile error about life cycle | |
// When arguments have &self, then returned value has the same life cycle with self | |
// However default_value can have longer or shorter lifecycle. | |
// pub fn get_or(&self, index: usize, default_value: &T) -> &T { | |
// match self.get(index) { | |
// Some(v) => v, | |
// None => default_value | |
// } | |
// } | |
// life time specifier 'a will define the lifecycle | |
// self and default_value are borrowed until the return type is alive | |
pub fn get_or<'a>(&'a self, index: usize, default_value: &'a T) -> &'a T { | |
self.get(index).unwrap_or(default_value) | |
} | |
fn grow(&mut self) { | |
todo!(); | |
} | |
fn allocate_in_heap(capacity: usize) -> Box<[T]> { | |
std::iter::repeat_with(Default::default) | |
.take(capacity) | |
.collect::<Vec<_>>() | |
.into_boxed_slice() | |
} | |
} | |
fn main() { | |
let mut v = ToyVec::new(); | |
v.push("ABC".to_string()); | |
v.push("DEF".to_string()); | |
// borrow value from v | |
let e0 = v.get(1); | |
assert_eq!(e0, Some(&"DEF".to_string())); | |
let default_value = &"OTHER".to_string(); | |
let e1 = v.get_or(10, default_value); | |
assert_eq!(e1, &"OTHER".to_string()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment