Advertisement
tomdodd4598

Untitled

Jun 6th, 2021
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.59 KB | None | 0 0
  1. fn insert_item<T>(start: &mut Option<Box<Item<T>>>, value: T, insert_before: fn(&Item<T>, &Item<T>) -> bool) where T: PartialEq + Display {
  2.     println!("Creating item: {}", value);
  3.     let mut item = Item::new(value);
  4.     let mut current = &*start;
  5.     let mut previous = &None;
  6.    
  7.     while current.is_some() && !insert_before(&item, current.as_ref().unwrap()) {
  8.         previous = current;
  9.         current = &current.as_ref().unwrap().next;
  10.     }
  11.    
  12.     item.next = *current;
  13.     let mut item = Some(Box::new(item));
  14.    
  15.     if previous.is_some() {
  16.         previous.as_mut().unwrap().next = item
  17.     }
  18.     else {
  19.         *start = item
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement