mikecargal

mikecargal

I get different results on puzzle 15

Rust Brain Teasers: description (Puzzle15: To Infinity)

On this line:

  fn fmt(&self, f: &mut fmt::Formatter<`_>) -> fmt::Result {

Formatter<`_> is flagged as an error (“this struct takes 0 generic arguments but 1 generic argument was supplied”).

When I “correct” it to f: &mut fmt::Formatter then the program runs just fine) only spits out one line and exits normally. It does not spit out node after node until it gets a stack overflow.

I do get a “dead code” warning on the prev property of Node, so maybe that’s being optimized out, thus avoiding your error?

Full source for reference:

use std::cell::RefCell;
use std::rc::Rc;

type Link = Option<Rc<RefCell<Node>>>;

// #[derive(Debug)]
struct Node {
    elem: i32,
    next: Link,
    prev: Link,
}

impl Node {}

use std::fmt;
impl fmt::Debug for Node {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "elem: {}", self.elem)
    }
}

fn main() {
    let mut head = Some(Rc::new(RefCell::new(Node {
        elem: 1,
        next: None,
        prev: None,
    })));
    head.as_mut().unwrap().borrow_mut().next = Some(Rc::new(RefCell::new(Node {
        elem: 2,
        next: None,
        prev: head.clone(),
    })));
    println!("{:?}", head);
}

also for reference:

rustup show
Default host: x86_64-apple-darwin
rustup home:  /Users/mike/.rustup

installed toolchains
--------------------

stable-x86_64-apple-darwin (default)
nightly-x86_64-apple-darwin

installed targets for active toolchain
--------------------------------------

wasm32-unknown-unknown
x86_64-apple-darwin

active toolchain
----------------

stable-x86_64-apple-darwin (default)
rustc 1.55.0 (c8dfcfe04 2021-09-06)

First Post!

herbert

herbert

Author of Hands-on Rust

Thanks! I’ve added that to the tracker. This is an odd one, I think an update changed some behavior - because I’m now getting a different result on my travel laptop (I’m taking a short holiday) and remote controlling my work setup. I’ll investigate further for B2. I’m leaning towards a source control merge issue, it looks like I have a branch merged on my work setup and not on the travel laptop - the source isn’t quite the same.

Where Next?

Popular Pragmatic Bookshelf topics Top

jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex&gt; product = %Product{} %Pento....
New
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
New
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New

Sub Categories: