Before diving into Rust, I used to dabble in D (http://www.dlang.org/). One of the features that stuck with me the most were the 'contracts' that checked inputs and outputs of the functions (Contract Programming - D Programming Language):
int fun(ref int a, int b)
in
{
assert(a > 0);
assert(b >= 0, "b cannot be negative!");
}
out (r)
{
assert(r > 0, "return must be positive");
assert(a != 0);
}
do
{
// function body
}
Quite similar to them are the 'invariants', which verify that certain conditions within a struct are met (Contract Programming - D Programming Language):
class Date
{
int day;
int hour;
this(int d, int h)
{
day = d;
hour = h;
}
invariant
{
assert(1 <= day && day <= 31);
assert(0 <= hour && hour < 24, "hour out of bounds");
}
}
Is there an alternative to them in Rust? If not, could it make sense to implement them as an additional part of the language?