Rust | Efficient | With Comments
Looking at other (some top-voted) Rust solutions here, it seems necessary to say a few words about efficiency. Using to_vec() to create new vectors in order to 
Looking at other (some top-voted) Rust solutions here, it seems necessary to say a few words about efficiency. Using `to_vec()` to create new vectors in order to satisfy the given function signature is wasteful, so we should delegate the recursion to a helper function that has a more efficient signature. But we should not take a `&Vec<i32>` in that signature, since this needs an additional pointer lookup compared to `&[i32]`. This is BTW one of the optimizations that `cargo clippy` would suggest if we would try to use the former argument type.\n\nAlso, it\'s quite unnecessary to first construct an empty node in an `Rc`, and then borrowing it to add the children. I instead construct the node, including the children, as an owned variable, and then move it into the wrapping `RefCell` etc in a single expression.\n\nMore a matter of style maybe, but I also prefer avoiding manual indexing when I can, and rather lean back on functions like `split_at(...)`. Less reinvention of the wheel, less error prone.\n\n```\nuse std::cell::RefCell;\nuse std::rc::Rc;\nimpl Solution {\n    fn recurse(nums: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n        if nums.is_empty() {\n            None\n        } else {\n            let (left, rest) = nums.split_at(nums.len() / 2);\n            let (curr, right) = rest.split_first().unwrap();\n            Some(Rc::new(RefCell::new(TreeNode {\n                val: *curr,\n                left: Self::recurse(left),\n                right: Self::recurse(right),\n            })))\n        }\n    }\n\n    pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {\n        Self::recurse(&nums)\n    }\n}\n```
['Rust']