5 ms·
What's a sane/legit use case of such inferring in general?
by zerr 4y ago
What's a sane/legit use case of such inferring in general?
- brundolf 4y agoI think the one here is legit (perhaps more realistic would be where you're passing the new array to an actual function that requires a certain array length). People always complain about Rust's type system being verbose- well, this makes it less verbose. It knows what you need from the function call, so it doesn't make you say it. And as an added benefit, if the required type ever changes in a way that can still be generated by the provided code (eg. the function you're passing to needs a different length), you won't need to update any explicit type annotations at the call-site. If that's ever undesirable - you want to be very precise about your code - well, then you can do that too by specifying the generic parameters explicitly.
- game-of-throws 4y agolet numbers = [0, 1, 2]; let doubled = numbers.map(|x| x * 2); You wouldn't want to have to explicitly type out the length of the second array.
- tomjakubowski 4y agoThat isn't really inferring the size of the array though. [T; N]::map(impl FnMut(T) -> U) defines its return type as [U; N]. N is fixed at the original array literal. https://doc.rust-lang.org/std/primitive.array.html https://doc.rust-lang.org/std/primitive.array.html
- brundolf 4y agoIt's inferring it from the array literal in the first place, but it is a different kind of inference from the original one. But I think that's what the question was asking for: > What's a sane/legit use case of such inferring in general? Depends how you interpret "such inferring"
- dymk 4y agoIt's inferring the size of `doubled`
- deleted 4y ago[deleted]
- Ar-Curunir 4y agoYou general type inference, or inferring the array length? The latter follows from the former, so it would be kind of a weird hole in the language if type inference worked everywhere except array lengths.
- zerr 4y ago"Scattered through multiple lines" inference :) Such code makes another programmer analyze multiple lines to deduce types in the head.
- brundolf 4y agoIn practice, type inference works best when paired with editor integration. Most languages/editors will give you hover-overs with inferred types, but some like rust-analyzer have started going further (the greyed-out text here is all inlaid by the editor): https://user-images.githubusercontent.com/3971413/96668192-1c51d500-13b7-11eb-9f17-cb5fe6be4605.png https://user-images.githubusercontent.com/3971413/96668192-1...
- dymk 4y agoThe tooling ecosystem, for instance rust-analyzer, make this not an issue if you're reading the code in an environment that supports it. I have rust-analyzer to display inferred types when I hold `control+command`. In other languages, this can be a brittle/expensive operation due to meh tooling. But with Rust, it tends to "just work". Online viewers of source e.g. Github and such don't have all this information exposed yet, but they're incrementally getting there. For instance, "go to definition" works for Rust code in Github. One could imagine a generic file format included as an artifact in source control, that online viewers could consume, to annotate spans of source code. Go to definition, show expanded type, etc.
- PoignardAzur 4y agoA few use cases: let foobar = Default::default(); do_something_with(foobar); // where do_something_with takes a FooBar param One I often use in unit tests: fn generate_ids<const N: usize>() -> [Id; N] { ... } let [id_button, id_label, id_thing, id_other_thing] = generate_ids(); (before const generics were stabilized, the above code instead had multiple utility functions, eg generate_2_ids, generate_3_ids, etc)
- kibwen 4y agoIt's not anything specific to arrays, it's just how type inference works. Here's another example: let mut x = HashMap::new(); x.insert(1, 2); Note that nowhere did we need to specify the concrete type of the HashMap; the compiler sees that you eventually insert numbers into the map so it uses that information to fill in the generic type parameters for the key and value.