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
this codes should be right like #2515 (comment)
but now cargo check report error
error: could not find defining uses
--> src/main.rs:5:14
|
5 | type Adder = impl Fn(usize) -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
error: could not compile `learn`
To learn more, run the command again with --verbose.
the cargo version is cargo 1.51.0-nightly (ab64d1393 2021-02-10)
Note how Adder now has a value. So this is fine, even though the value is provided in a different spot from the declaration.
3 - compiles type alias
typeRR<T> = Result<T,Infallible>;
is similar to
letAdder:implFn(usize) -> usize = func;
Note how the declaration and definition are done at the same time. So this is also fine. There are two big differences between normal variables, and top-level declarations:
Top-level declarations can be in an arbitrary order, where as normal variables must be declared before or at the same time as the they are defined/used.
Top-level declarations must have a defined value (even if unused), whereas normal variables are allowed to be undefined (as long as they aren't used).
Activity
RustyYato commentedon Feb 14, 2021
That isn't a complete snippet, you also need a defining use, i.e. a function return type.
Sherlock-Holo commentedon Feb 14, 2021
I try this
now cargo check doesn't report errors, but why I have to use it?
If I write
type RR<T> = Result<T, Infallible>;cargo check won't force me to use itRustyYato commentedon Feb 14, 2021
I think this will be easier to understand via analogy.
1 - does not compile
type_alias_impl_traitis similar to
Note how
Adderis declared, but has no value. Now, Rust enforces that all top-level declarations must have a value. So leaving it how doesn't work.2 - compiles
type_alias_impl_traitis similar to
Note how
Addernow has a value. So this is fine, even though the value is provided in a different spot from the declaration.3 - compiles
typealiasis similar to
Note how the declaration and definition are done at the same time. So this is also fine. There are two big differences between normal variables, and top-level declarations:
scottmcm commentedon Mar 31, 2021
It looks like this is working as expected, then, so I'll close this issue.