Context
Rust does a lot of smart things to determine the types of variables. This is why you rarely need to define the types of a variable explicitly. Rust can search very far to find a type for a function. For example, this is invalid:
let i;
but this is valid:
let i;
i=5
These lines do not need to be next to eachother. There can be a lot of code in between.
Problem
Consider this rust answer:
|a,b|a;
This won't compile. You need to specify the type for a
and b
. To make this compile you would need
|a:u8,b:u8|a;
However, often rust answers have test cases, so the full code will look more like this:
fn main(){
f =
|a,b|a;
asserteq!(f(1,2), 1);
}
This compiles, the test case in this case tells rust the type of the function, even though it appears much later in the code.
Many answers use this trick so save bytes, for example:
- It's just Rocket Science Here the type of
m
is unspecified. - Do 2 numbers contain unique powers of 2 Both
a
andb
are missing types - All Possible Ties in Tic-Tac-Toe My own answer
- How many light changes will a 7-segment display need?
- Finding prime numbers without using "prime characters" The TIO link for this answer even explicitly types the function but doesn't include that in the byte count
Not criticizing any of these answers, there where all valid when posted.
There might also be cases when the test cases accidentally limit the return type too, or the type of a global variable. Just listing those for argument types since the issue is easier to spot but it's not the only way the type can be defined outside the block itself.
Question
Should it be allowed for the types of arguments in rust (and maybe other languages that have implicit static typing) be allowed to derive from test cases?