Puzzle 15To 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

images/aside-icons/important.png

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. ...

Get Rust Brain Teasers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.