Noob: Syntax? - Let new_x = || { x::new() }?


#1

What is the || in this syntax suggesting?

let new_set = || { Set::<Name>::new() };

#2

That new_set will be a closure that doesn’t take any argument and with an inferred return type. Similar to a function fn() -> T { Set::<Name>::new() }, but with access to the local stack and where T is the return type of Set::<Name>::new(). You can read more about them in The Book.


#3

The usage would then be

let new_set = || { Set::<Name>::new() };

let actual_value = new_set();
let value2 = new_set();

#4

Yeah, I was wrong about not returning anything. The return type is from the block.


#5

Just edit it, nobody will ever know.


#6

What are you talking about? What am I talking about?!


#7

Something about an edit… which nobody might spot, if they know to click on the pencil top tight of the post showing the diff and history. :open_mouth:


#8

That pencil is purely decorative :wink:


#9

It is an easter egg. If you watch it long enough it will transform into a golden coin which drops onto your desk.


#10

Cool! I will keep this coin in a special place :smile: