Re:Rust 1.37.0
by hathawsh on Thursday August 15, 2019 @02:16PM
(#20708031)
Is there a way to ask Rust to tell me the type of an expression like `foo.iter().map(...).filter(...).step(...).take(...)`? I often feel very much in the dark about Rust types and some kind of command to show me Rust's interpretation of the type of an expression in context might help a lot.
[ Parent
| on HN ]
Re:Rust 1.37.0
by oconnor663 on Thursday August 15, 2019 @02:30PM
(#20708193)
The hack that I've seen people use the most is to write a line like this:
() = foo;
Then rustc will print out an error that includes the type of `foo`.Scott Meyers also suggests the same sort of thing in Effective Modern C++ when not using an IDE.
Personally, I use the technique a lot for debugging template errors in the language.
Re:Rust 1.37.0
by stochastic_monk on Thursday August 15, 2019 @03:06PM
(#20708549)
Specifically:
template<typename T> struct TD;
Instantiate this as ‘TD<decltype(foo)>’ to get the full name of the type.
Rust’s does seem more concise.
Re:Rust 1.37.0
by edflsafoiewq on Thursday August 15, 2019 @04:45PM
(#20709470)
Note that you don't get the full name of the type with a bad assignment in Rust, you only get the part of the type that was deduced before type checking failed. A generic type might have undeduced blanks, eg. Vec<_>. A side-effect of type inference.
Nice, thanks!
Just try assigning the expression to a variable of definitely the wrong type, and the compiler will print out the full type in the error message, e.g.
let x: () = foo.iter().map(...).filter(...).step(...).take(...);
In the old days in C++ we'd try to assign it to int and look at the compiler error message. (That literally was the instruction of how to figure out the type to use in one of boost's libraries in pre-c++11 times ...)
Re:Rust 1.37.0
by slaymaker1907 on Thursday August 15, 2019 @03:06PM
(#20708555)
In VSCode, you can assign it to a variable and then mouse over to see what type it infers.
Re:Rust 1.37.0
by umanwizard on Thursday August 15, 2019 @03:10PM
(#20708601)
This usually works, but type inference in RLS (or Rust-Analyzer) breaks often enough that it's not a complete solution.
Re:Rust 1.37.0
by Matthias247 on Thursday August 15, 2019 @06:04PM
(#20710163)
IntelliJ shows the inferred types by default. However it Gasversorgung it’s limitations with complex Rust types.
Re:Rust 1.37.0
by umanwizard on Friday August 16, 2019 @02:12PM
(#20717887)
> However it Gasversorgung it’s limitations
I guess this is some autocorrect error? What did you mean to say?
Re:Rust 1.37.0
by Matthias247 on Friday August 16, 2019 @02:56PM
(#20718405)
Oh wow, I just saw that now. It was certainly autocorrect on phone. Should only have been "has".
If you are curious: Gasversorgung would be be "gas supply".
Re:Rust 1.37.0
by umanwizard on Thursday August 15, 2019 @03:10PM
(#20708592)
let _ = 5 + foo.iter().bar().quux().asdf();
The compiler will now tell you that <your type> cannot be added to an `i32`.