The Benefits of Rust and How it Scores Over C

Rust is a mod­ern sys­tems pro­gram­ming lan­guage that pri­mar­ily aims at us­ing code that is mem­ory safe. It has a lot of new fea­tures that are not avail­able in the C pro­gram­ming lan­guage. Rust makes dan­gling point­ers im­pos­si­ble. It also tests the code ex­am­ple

OpenSource For You - - CONTENTS - Ref­er­ences [1] http://doc.rust-lang.org/book/README.html [2] http://www.rust-lang.org/ [3] http://web.mit.edu/rust-lang_v0.11/doc/tu­to­rial.html [4] https://github.com/rust-lang/rust By: Anu V. The au­thor is a FOSS en­thu­si­ast and blogs at https://cap­tanu.w

Rust is a sys­tems pro­gram­ming lan­guage that is de­signed for writ­ing sys­tems soft­ware com­piled with the self­hosted com­piler known as rustc, with the low level vir­tual ma­chine (LLVM) as its back­end. The com­piler is writ­ten in the Rust pro­gram­ming lan­guage and is de­vel­oped by the Mozilla com­mu­nity. It en­sures mem­ory safety by set­ting the in­vari­ant in such a way that the cre­ation of dan­gling point­ers is not pos­si­ble. It is also type safe, that is, it pre­vents type er­rors. Rust is a multi-par­a­digm lan­guage that sup­ports pro­ce­dural, func­tional and ob­ject-ori­ented styles as well as con­cur­rent com­pu­ta­tions. It is de­signed in such a way that it can be used to cre­ate large client and server pro­grams that run over the In­ter­net.

Mem­ory safety

In soft­ware devel­op­ment, mem­ory safety is con­sid­ered to be an im­por­tant fac­tor as it pre­vents soft­ware er­rors that can cause se­cu­rity vul­ner­a­bil­i­ties through RAM ac­cesses. Data race and dan­gling point­ers are some of the mem­ory er­rors that are com­mon to most of the lan­guages. Dan­gling point­ers do not oc­cur in Rust be­cause of the spe­cial types of point­ers used in it, called smart point­ers. Mem­ory al­lo­cated to smart point­ers is freed only when they go out of scope, hence avoid­ing the for­ma­tion of dan­gling point­ers, which point to a de-al­lo­cated mem­ory.

The data in Rust is im­mutable, by de­fault, and can be made mu­ta­ble by adding the key­word mut. Rust does not al­low the shar­ing of mu­ta­ble data be­tween threads; hence, it elim­i­nates race con­di­tions for in-mem­ory data. In­stead of shar­ing, data is copied or owned point­ers are moved be­tween tasks. In ad­di­tion to this, Rust en­sures that data is ini­tialised be­fore it is read from any­where in the pro­gram and also does ‘bound check­ing’.

In­stal­la­tion in Linux and Mac

You can use the rustup Mac, as fol­lows:

script to in­stall Rust in Linux and

To build from the repos­i­tory, type:

Af­ter the Rust source code is avail­able, con­fig­ure it and build us­ing the fol­low­ing com­mands:

When the make in­stall is com­plete, sev­eral pro­grams will be placed in /usr/lo­cal/bin like rustc, the Rust com­piler and rust­doc, which is the API doc­u­men­ta­tion tool.

In­stal­la­tion in Win­dows

The pre­com­piled bi­nary in­stall­ers for Win­dows are avail­able at the Rust web­site, from where it can be down­loaded. Af­ter do­ing so, run the .exe file, which com­pletes the in­stal­la­tion of Rust.

You can check whether the in­stal­la­tion is com­plete by typing the fol­low­ing com­mand: Note: Rust cur­rently needs about 1.5GB of RAM to build with­out swap­ping; if it uses swap dur­ing the build process, it will take a very long time to build.

Com­pil­ing and run­ning the pro­gram

Cre­ate a file hel­lo_­world.rs and in­clude the fol­low­ing lines:

The Rust pro­gram can be com­piled us­ing the fol­low­ing com­mand:

When the pro­gram is com­piled suc­cess­fully, Rust will gen­er­ate a bi­nary ex­e­cutable au­to­mat­i­cally, with the name hel­lo_­world which can be run as usual, as shown be­low:

Cargo

Cargo is a tool to man­age Rust projects, which has the fol­low­ing func­tion­al­i­ties: Builds the code Down­loads the re­quired de­pen­den­cies for your code Builds the de­pen­den­cies To ‘cargo-ify’ the project, two steps are to be fol­lowed: Make a con­fig­u­ra­tion file, Cargo.toml Put all the source files in the src folder Given be­low are the con­tents of the Cargo.toml file:

The project can be built and run as fol­lows:

When the Cargo build is run, you will no­tice that a file, cargo.lock, is cre­ated. This file is used by Cargo to keep track of de­pen­den­cies in the project.

Rust ver­sus C

Even though both the pro­gram­ming lan­guages sup­port man­ual mem­ory man­age­ment, they work dif­fer­ently. In C, the mem­ory is al­lo­cated and freed by us­ing spe­cial func­tions like

mal­loc() and free(). The mal­loc func­tion al­lo­cates mem­ory and re­turns a void pointer point­ing to the al­lo­cated space. Af­ter the mem­ory is freed, the vari­able point­ing to that mem­ory is called a dan­gling pointer. Us­ing dan­gling point­ers causes se­cu­rity vul­ner­a­bil­i­ties and the C com­piler does not pro­vide any method of avoid­ing them.

In con­trast to C, the Rust lan­guage does not sup­port dan­gling point­ers. The al­lo­ca­tion and ini­tial­i­sa­tion of the mem­ory is done at the same time us­ing the al­lo­ca­tion op­er­a­tor ‘~’ , which re­turns a smart pointer. The com­piler will au­to­mat­i­cally free the mem­ory when the pointer goes out of scope. The pro­gram­mer need not do it man­u­ally and hence avoids dan­gling point­ers from be­ing cre­ated.

Ex­pres­sions

Rust is an ex­pres­sion based lan­guage. We can also say that the fun­da­men­tal dif­fer­ence be­tween the Rust syn­tax and lan­guages like C is that some things that are state­ments in C are ex­pres­sions in Rust. The com­mand ‘if’ is an ex­pres­sion whereas ‘let’ is a state­ment in Rust.

In C, if we want to as­sign a value to a vari­able based on some con­di­tion, it is done in the fol­low­ing way:

The same thing can be done in Rust, as fol­lows:

Here, the ‘if’ con­di­tion is an ex­pres­sion; it re­turns some value, depend­ing on the con­di­tion that holds. It is less er­ror-prone and is more com­fort­able. But Rust also sup­ports the com­mon syn­tax and hence the coder can use ei­ther of the two ways.

Rust doc­u­men­ta­tion

Rust­doc is a build in the API-doc­u­men­ta­tion tool in Rust, which gen­er­ates HTML doc­u­men­ta­tion from doc com­ments. In or­der to gen­er­ate doc­u­men­ta­tion, it is nec­es­sary to in­clude some doc­u­men­ta­tion re­lated lines in­side the code.

Cre­at­ing the doc­u­men­ta­tion

When some­thing has to be doc­u­mented, we can use doc com­ments. The fol­low­ing is a sam­ple set of lines that de­scribes the doc­u­men­ta­tion func­tion­al­ity writ­ten in my­world.rs file:

The HTML doc­u­men­ta­tion for the above file can be

gen­er­ated by run­ning the fol­low­ing com­mand:

When the com­mand is run, a doc di­rec­tory is cre­ated, by de­fault, which will have a my­world di­rec­tory con­tain­ing HTML files. The src folder will have the file that con­tains the lines that we have writ­ten for gen­er­at­ing the doc­u­men­ta­tion. The Web pages gen­er­ated by rust­doc are in the same log­i­cal hi­er­ar­chy that is fol­lowed when writ­ing a li­brary. All the items like struct and func­tions will have a unique colour and when clicked on, you will be di­rected to their re­spec­tive doc­u­men­ta­tion.

Testing the doc­u­men­ta­tion

In ad­di­tion to sup­port for doc­u­men­ta­tion, Rust­doc also ver­i­fies the code ex­am­ples that are in­cluded in the doc­u­men­ta­tion. For that we just need to in­clude the --test op­tion while run­ning the rust­doc com­mand.

Rust­doc also pro­vides few ex­tra fea­tures that sim­plify some te­dious doc­u­men­ta­tion ex­am­ples. If a line is pre­fixed with #, then that line will not show up in the HTML doc­u­men­ta­tion but it will be used when testing the code block.

Fig­ure 2 : In­dex page

Fig­ure 3: Func­tions page

Fig­ure 1: In­stal­la­tion in Linux

Fig­ure 5: Re­sults of us­ing --test

Fig­ure 4: Wid­gets page

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.