Advertisement
tomdodd4598

Untitled

Jun 6th, 2021
1,397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.60 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 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.     let mut item = Some(Box::new(item));
  13.     if previous.is_some() {
  14.         previous.as_mut().take().unwrap().next = item
  15.     }
  16.     else {
  17.         *start = item
  18.     }
  19.     item.unwrap().next = current.take();
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement