You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, functions must always return one singular type, and as such all || {} produced by new_closure() must be of the same type.
Personally this surprised me because I originally thought that all closures are unique from each other, but this assumption can be broken by returning closures from functions (or even closures!).
Another fun exercise:
use std::any::Any;fnf<T,U>(t:T,u:U)whereT:'static,U:'static,{match t.type_id() == u.type_id(){true => print!("1"),false => print!("0"),}}fneq<T:PartialEq>(t0:T,t1:T){match t0 == t1 {true => print!("1"),false => print!("0"),}}fnc() -> implFn(){
|| {}}fnd(x:i32) -> implFn() -> i32{move || x
}fnmain(){let x = d(1);let y = d(2);let a = x();let b = y();f(|| {}, || {});f(c(),c());f(x, y);eq((|| {})(),(|| {})());eq(c()(),c()());eq(a, b);}
Answer: 011110
The text was updated successfully, but these errors were encountered:
Answer:
01
Hint
Do closures have the same type? How many types can a function return?
Explanation
According to the Rust Reference Book, "A closure expression produces a closure value with a unique, anonymous type that cannot be written out."
However, functions must always return one singular type, and as such all
|| {}
produced bynew_closure()
must be of the same type.Personally this surprised me because I originally thought that all closures are unique from each other, but this assumption can be broken by returning closures from functions (or even closures!).
Another fun exercise:
Answer:
011110
The text was updated successfully, but these errors were encountered: