HOW TO START US­ING RUST

Web Designer - - Get started with rust -

“A FEA­TURE called BOR­ROW­ING AL­LOWS THE PRO­GRAM­MER TO TRANS­FER OWN­ER­SHIP be­tween RE­SOURCES”

While pack­age man­agers such as ‘apt-get’ surely made de­vel­oper's life eas­ier, pack­age list main­tain­ers are, by and large, known to be no friends of ex­ces­sive speed. Be­cause of that many, if not most, dis­tri­bu­tions’ pack­age caches are heav­ily out­dated.

The Rust team has solved this prob­lem by pro­vid­ing a ded­i­cated in­stal­la­tion script, which sets up the sys­tem it is run on ef­fi­ciently. De­ploy­ment, then, is a two-step process: first, make sure that the CURL down­loader/parser is avail­able:

tamhan@tamhan-thinkpad:~$ sudo apt-get in­stall curl

[sudo] pass­word for tamhan: Read­ing pack­age lists… Done…

Then, in the sec­ond step, down­load the in­staller and pass it on to ‘sh’ us­ing the pipe op­er­a­tor. ‘Sh’ is a short­hand for the de­fault shell in­ter­preter of your work­sta­tion, which will then pro­ceed to run the code at hand:

tamhan@tamhan-thinkpad:~$ curl https://sh.rustup.rs -ssf | sh info: down­load­ing in­staller

Dur­ing the in­stal­la­tion, the Rust in­staller will dis­play a va­ri­ety of prompts sim­i­lar in ap­pear­ance to the ones shown in the pic­ture on the right. Sim­ply follow them to achieve a de­fault in­stal­la­tion.

In some cases, the Rust de­ploy­ment will fail with an er­ror which is sim­i­lar to “er­ror: could not write rc­file file: ‘/home/ tamhan/.bash_pro­file’ ”. If this hap­pens dur­ing in­stal­la­tion, use ‘sudo -s’ to get a root shell, and then rinse and re­peat to pro­ceed.

RUN A Sam­ple!

With that out of the way, it is time to run our first small pro­gram. Rust files, by de­fault, have the file ex­ten­sion ‘.rs’. Cre­ate a file called ‘firsttest.rs’, and then pro­vide it with the fol­low­ing piece of code:

fn main() { println!("hello World!"); } C and C++ pro­gram­mers of­ten won­der why in­vo­ca­tions of the ‘println’ method re­quire the use of an ex­cla­ma­tion mark. The an­swer is sim­ple: Rust im­ple­ments ‘println’ as a macro, which gets in­voked in a dif­fer­ent way.

Given that Rust is a com­piled lan­guage, our ex­am­ple must be pro­cessed be­fore it can be run from the com­mand line:

tamhan@tamhan-thinkpad:~/ rustspace$ rustc firsttest.rs tamhan@tamhan-thinkpad:~/ rustspace$ ./firsttest

Hello World!

Ad­vanced de­cay!

De­scrib­ing a com­plete pro­gram­ming lan­guage, such as Rust, in the lim­ited space we have avail­able here is im­pos­si­ble. So let’s start by for­mally point­ing you to Mozilla Re­search’s com­pre­hen­sive doc­u­men­ta­tion at rust­byex­am­ple.com and shown on the first page. One fea­ture which is sure to im­press C pro­gram­mers in­volves the use of the ‘match’ com­mand: in ad­di­tion to di­rect com­par­isons, it also en­ables the use of range op­er­a­tors to greatly sim­plify the de­sign of ad­vanced pro­grams:

fn main() { let num­ber = 5; println!("work­ing on {}", num­ber); match num­ber {

1 => println!("one!"), 2 | 3 | 5 | 7 => println!("prime"),

_ => println!("a num­ber"),

} }

GARBAGE-AT-HAND!

Clas­sic garbage collection has its weak­nesses: most im­ple­men­ta­tions bring the pro­gram to a screech­ing halt from time to time. To get around that Rust uses a set of so-called ‘zero-cost ab­strac­tions’ to emu­late a sim­i­lar be­hav­iour in a less an­noy­ing fash­ion.

In prin­ci­ple, ev­ery re­source is cre­ated with an owner in a fash­ion sim­i­lar to Qt's par­ent-child sub­sys­tem. How­ever, a fea­ture called ‘bor­row­ing’ en­ables the pro­gram­mer to trans­fer own­er­ship be­tween re­sources tem­po­rar­ily, thereby pass­ing them around the sys­tem.

Un­for­tu­nately, a com­plete dis­cus­sion of the pos­si­bil­i­ties of Rust’s mem­ory man­ager would break the lim­its of this ar­ti­cle – let it suf­fice to de­clare that Rust pro­vides a very un­ortho­dox, but work­able form of mem­ory man­age­ment.

The RUST in­staller pro­vides a semi-graphic in­stal­la­tion en­vi­ron­ment

Newspapers in English

Newspapers from UK

© PressReader. All rights reserved.