Puzzle 15To Infinity
 use​ ​std​::​cell​::RefCell;
 use​ ​std​::​rc​::Rc;
 type​ Link = Option<Rc<RefCell<Node>>>;
 struct​ Node {
  elem: i32,
  next: Link,
 fn​ ​main​() {
 let​ ​mut​ head = ​Some​(​Rc​::​new​(
 RefCell​::​new​(Node{ elem: 1, next: None })
  .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. ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.