More on Rust

bearophile bearophileHUGS at lycos.com
Wed Feb 9 16:23:19 PST 2011


The Development of the Rust language from Mozilla and Graydon Hoare is going on. It will probably become a quite interesting system language, quite more interesting than Go. One of the most interesting features of Rust will be its typestate.

Rust has structural typing too, this is sometimes useful (but to use it well you need something like pattern matching used in functional languages like ML, that I don't know if is present in Rust).


Graydon is following D idea of putting a basic unit testing feature in the language:
https://mail.mozilla.org/pipermail/rust-dev/2010-December/000160.html
https://mail.mozilla.org/pipermail/rust-dev/2010-December/000163.html
https://mail.mozilla.org/pipermail/rust-dev/2010-December/000165.html
Rust also puts basic logging as a built-in feature.


This is a blog post about Rust type inference:
http://pcwalton.blogspot.com/2010/10/rust-features-i-type-inference.html

Rust doesn't have a whole program type inferencer (as ML), like a Hindley-Milner algorithm, to keep things simple and allow separate compilation (and probably to keep compilation times low, etc). So you have to give type to function arguments, and if you want you are free to use "auto" everywhere inside the function to infer types locally. I think this is good. So on this it's quite similar to D, but there are some differences.

Rust allows to do this ("log" is a built-in logging feature, I don't know how they call the logarithm function):


auto x;
if (localtime().hours >= 8) {
    x = "awake!"
} else {
    x = "asleep, go away."
}
log "I'm " + x;


>From the blog post:

>Rust features what I call feed-forward type inference, which is summed up by this rule: Variables have no type until they are assigned a value, at which point their type can't change.<

>Here, we didn't initialize x in its declaration, but this code still compiles and runs properly. This is because Rust is able to wait until x is assigned to determine its type. Unlike, say, C# (and, of course, languages like ML in which variables must be assigned a value at declaration time), Rust doesn't require that auto declarations be initially assigned a value to determine a type for them. At the same time, unlike C, the compiler will emit an error if you ever forget to assign a value to the variable in any branch of your code. For instance, if you omit the else branch above, the code won't compile.<


Rust is statically typed, so this is an error, it's not possible (unless you use some kind of variant):

log "do you want to add strings or ints?";
auto use_str = input.readline == "strings";
if (use_str) {
    a = "hello ";
    b = "world!";
} else {
    a = 1;
    b = 1;
}
log a + b;


Both branches of the "if" must assign the same types to a and b, like when in D you use an auto return type, and the D compiler makes sure all your return statements return exactly the same type.


"Variables have no type until they are assigned a value, at which point their type can't change." looks meaningless if you see types as timeless things, that are are always true and immutable for a variable. But in Rust there are typestates, so while a variable can't change type, its type sometimes changes state along the flow of the code, such state of the type may be different in different parts of the code.

Bye,
bearophile


More information about the Digitalmars-d mailing list