Cre­ate A NEW PROJECT

Web Designer - - Get started with rust -

While C and C++ code can be bro­ken down into li­braries, do­ing so is an an­noy­ing and some­what un­com­fort­able task. Fur­ther­more, mak­ing sure that all the re­quired li­braries are where they are needed is some­thing even sea­soned de­vel­op­ers like to avoid.

The Javascript com­mu­nity has long solved this prob­lem via prod­ucts such as NPM. Projects are de­scribed via a project struc­ture file, which – among other things – con­tains ref­er­ences to li­braries and other el­e­ments needed dur­ing the ac­tual com­pi­la­tion run.

In the case of Rust, a pack­age man­ager called Cargo does a sim­i­lar job. Cre­at­ing a new project can be ac­com­plished through the use of the ‘cargo new’ com­mand in a fash­ion sim­i­lar to the fol­low­ing:

tamhan@tamhan-thinkpad:~/ rustspace$ cargo new fu­ture­sam­ple1 --bin

Cre­ated bi­nary (ap­pli­ca­tion) `fu­ture­sam­ple1` project tamhan@tamhan-thinkpad:~/ rustspace$

When the cre­ation process is com­plete, a folder struc­ture sim­i­lar to the one shown in the pic­ture at the bot­tom of this page will be gen­er­ated.

.toml files act as ‘con­trollers’ – in the case of our newly-gen­er­ated project, the file con­tains the fol­low­ing struc­ture:

[pack­age] name = "fu­ture­sam­ple1" ver­sion = "0.1.0" au­thors = ["tamhan"] [de­pen­den­cies]

Sim­i­lar­i­ties to the .ini files of lore are not purely co­in­ci­den­tial: in a fash­ion not dis­sim­i­lar to NPM, the an­cient con­fig­u­ra­tion file for­mat is also used to de­scribe the con­fig­u­ra­tion files used for Rust projects. The ‘[de­pen­den­cies]’ block is of spe­cial in­ter­est for us – it con­tains a list of all ex­ter­nal li­braries, which need to be present for the com­pi­la­tion to suc­ceed.

Add A packet!

Cre­at­ing a web server by hand is an un­grate­ful and an­noy­ing job best left to masochists. We will, in­stead, opt for a frame­work. Sadly, find­ing the right one is not easy – as shown in the pic­ture above, vis­it­ing arewewe­byet. org/top­ics/frame­works/ re­veals an ex­ces­sively long list of can­di­dates.

We will pick Rocket, if only be­cause it seems to be quite pop­u­lar and has seen an up­date re­leased in Jan­uary of 2018. Sadly, Rocket’s de­vel­op­ers tend to take a lik­ing to newly-in­tro­duced lan­guage fea­tures, which is why fre­quent up­dat­ing of your Rust in­stal­la­tion us­ing the fol­low­ing com­mands is re­quired:

tamhan@tamhan-thinkpad:~/ rustspace/fu­ture­sam­ple1$ rustup up­date && cargo up­date

The next step largely is a ques­tion of taste. Most li­braries come with pre-pro­vi­sioned starter projects, which de­vel­op­ers can sim­ply siphon from Github. Do­ing so for your ‘prime’ li­brary is not nec­es­sar­ily a bad idea – although once more than one li­brary is in­volved, a man­ual ap­proach tends to be more fruit­ful.

Next, open the .toml file, then mod­ify the ‘de­pen­den­cies’ sec­tion as per the fol­low­ing in or­der to in­clude a re­cent ver­sion of the Rocket frame­work:

[de­pen­den­cies] rocket = "0.3.6" rock­et_­code­gen = "0.3.6"

Rocket is unique in that it re­quires the in­clu­sion of a to­tal of two pack­ages: in ad­di­tion to the main frame­work, a sep­a­rate code gen­er­a­tor file is also re­quired. Ei­ther way, our ver­sion in­cludes a spe­cific ver­sion of the two li­braries – Cargo can also ac­cept wild­cards, which en­able the pro­gram to ‘pick its poi­son’ with­out any help from us.

With that out of the way, one prob­lem re­mains: enter ‘cargo run’ in the folder con­tain­ing the .toml file in or­der to per­form an as­sisted com­pile, which will – among other things – down­load the rel­e­vant code li­braries from the repos­i­tory and com­pile the whole en­chi­lada for you.

Sim­i­lar­i­ties to NPM are purely co­in­ci­den­tial...

Newspapers in English

Newspapers from UK

© PressReader. All rights reserved.