rustborrow-checkerownership

create ListNode from Vec with ref to first Node


#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
    pub val: i32,
    pub next: Option<Box<ListNode>>,
}

impl ListNode {
    #[inline]
    fn new(val: i32) -> Self {
        ListNode { next: None, val }
    }
}


fn create_list(list : Vec<i32>) -> Option<Box<ListNode>> {
    // the code should be written this place
}


i have ListNode struct like this and dont't want to edit this struct and impl. i get a list of i32 number as Vec and i want to create ListNode that each item in list point next item and at the end return first number

for example get vector like this [1,2,3] and at the end return object like this (i want to create programmatically and this is hand made)

ListNode { 
   val : 1,
   ListNode { 
      val : 2,
      next:ListNode { 
          next: None,
          val : 3
      }
   }
}

i can't hold first ListNode and return that while dealing with rust borrowing and ownership

-- Update --

i am new to rust. so i create an example from the way i want to solve this problem. this example written in typescript

class ListNode {
     val: number
     next: ListNode | null
     constructor(val?: number, next?: ListNode | null) {
         this.val = (val===undefined ? 0 : val)
         this.next = (next===undefined ? null : next)
     }
     toString(): string {
        return `ListNode ${this.val}${this.next===null? "" : '  => '+ this.next.toString()}`
     }
 }


function createList(list : number[]) : ListNode|null {
    const firstItem = new ListNode(0,null);
    let second = firstItem;
    for (let item of list) {
        second.next = new ListNode(item,null)
        second = second.next;
    }
    return firstItem.next;
}

const firstNodePointer = createList([1,2,3,4,5])

console.log(firstNodePointer?.toString())

Solution

  • This will create the list in one go.

    fn create_list(list : Vec<i32>) -> Option<Box<ListNode>> {
        let mut head = None;
        let mut current = &mut head;
        for x in list {
            *current = Some(Box::new(ListNode::new(x)));
            current = &mut current.as_mut().unwrap().next;
        }
        head
    }