Content
@
0 reply
0 recast
0 reaction
Agost Biro
@agostbiro
Let’s try a more difficult one with closures. What’s wrong with this closure let closure = |x: &str| x;
6 replies
0 recast
4 reactions
franco
@francos.eth
You would need to specify a lifetime since you’re returning a borrowed reference. I don’t remember right now if you can annotate lifetimes in closures 😅 but you could do: let closure = |x: &str| x.to_owned();
2 replies
0 recast
1 reaction
Agost Biro
@agostbiro
Very warm! Why does this work though? fn foo(s: &str) -> &str { s }
2 replies
0 recast
1 reaction
Adophilus
@adophilus
Because the compiler is smart enough to infer the returned reference lifetime... Because there is only one borrowed reference as a parameter
1 reply
0 recast
1 reaction
Agost Biro
@agostbiro
Right, but why can’t it do it for the closure?
0 reply
0 recast
0 reaction