Baffled yet again

They are different types! They live on the stack, so their size must be known; but their size differs between different closures, depending on what they are closed over. You can erase these differences by hiding them behind pointers as trait objects; &Fn, &mut FnMut, and Box<FnMut> are all ways that you can have a closure resolved dynamically. (also, closures with no context can coerce to the function pointer type fn(args) -> out).

let f: Box<Fn()> = match should_do_it {
   true => Box::new(|| { do_it(x); }),
   false => Box::new(|| {}),
};
    
let g: fn() -> _ = match output_3 {
   true => || 3,
   false => || 2,
};

This can be opaque to optimization, however, and it would seem a wholly inappropriate default behavior to wrap closures in general when we would like to have e.g. iterator chains compile down to something as fast as a hand written loop.

1 Like

https://github.com/azerupi/mdBook/issues/51

Until then, you can also control-f in the print view; click the print button in the upper right hand side.

And here's an example without boxes:

let c1;
let c2;
let f: &Fn() = match should_do_it {
   true => { c1 = || { do_it(x); }; &c1 },
   false => { c2 = || {}; &c2 },
};

I disagree with "Having a search bar would be very very useful". It's essential.

Good to know about searching the print view. But why hide it here? Why don't you say that on the very first page of Book 2, perhaps in the banner that says "You are reading a draft of the next edition of TRPL. For more, go here." Add "To search, click the Print icon and use your browser's search capability (ctrl-f)".

Because the intro chapter is the last to be re-done, and the old book didn't have a print view, so that would have made no sense when it was written.

I suggested doing it in the banner that says "You are reading a draft of
the next edition of TRPL. For more…". That banner wasn't in the Intro of
the first book; it's not in the text of the Introduction -- it's been added
in the second book. So why can't the banner be modified now? I don't
understand your response, particularly what this has to do with re-doing
the introductory chapter.

When I read this, it felt like you were accusing us of deliberately obfuscating this functionality, which I assure you was an accidental oversight, not an act of malice.

carols10cents https://users.rust-lang.org/u/carols10cents
October 5

donallen:

Good to know about searching the print view. But why hide it here?

When I read this, it felt like you were accusing us of deliberately
obfuscating this functionality, which I assure you was an accidental
oversight, not an act of malice.

No, I was not accusing you of any such thing! I stated that being able to
search the Book(s) is essential and Steve explained that there is a method,
albeit indirect, for doing so. My point was that his suggested workaround
should be available to everyone, not just people who happen to read this
conversation.

1 Like