February 2022
Intermediate to advanced
140 pages
2h 28m
English
| Puzzle 15 | To Infinity |
| | use std::cell::RefCell; |
| | use std::rc::Rc; |
| | |
| | type Link = Option<Rc<RefCell<Node>>>; |
| | |
| | #[derive(Debug)] |
| | struct Node { |
| | elem: i32, |
| | next: Link, |
| | } |
| | |
| | fn main() { |
| | let mut head = Some(Rc::new( |
| | RefCell::new(Node{ elem: 1, next: None }) |
| | )); |
| | head |
| | .as_mut() |
| | .unwrap() |
| | .borrow_mut() |
| | .next = Some(Rc::new(RefCell::new( |
| | Node{ elem: 2, next: head.clone() }) |
| | )); |
| | |
| | println!("{:?}", head); |
| | } |
Guess the Output | |
|---|---|
|
|
Try to guess what the output is before moving to the next page. |
The program will display node 1, node 2, and then node 1 again. ...
Read now
Unlock full access