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

#1

What is the || in this syntax suggesting?

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

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

2 Likes

#3

The usage would then be

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

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

#4

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

0 Likes

#5

Just edit it, nobody will ever know.

0 Likes

#6

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

1 Like

#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:

0 Likes

#8

That pencil is purely decorative :wink:

0 Likes

#9

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

1 Like

#10

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

0 Likes