Create A NEW PROJECT
While C and C++ code can be broken down into libraries, doing so is an annoying and somewhat uncomfortable task. Furthermore, making sure that all the required libraries are where they are needed is something even seasoned developers like to avoid.
In the case of Rust, a package manager called Cargo does a similar job. Creating a new project can be accomplished through the use of the ‘cargo new’ command in a fashion similar to the following:
tamhan@tamhan-thinkpad:~/ rustspace$ cargo new futuresample1 --bin
Created binary (application) `futuresample1` project tamhan@tamhan-thinkpad:~/ rustspace$
When the creation process is complete, a folder structure similar to the one shown in the picture at the bottom of this page will be generated.
.toml files act as ‘controllers’ – in the case of our newly-generated project, the file contains the following structure:
[package] name = "futuresample1" version = "0.1.0" authors = ["tamhan"] [dependencies]
Similarities to the .ini files of lore are not purely coincidential: in a fashion not dissimilar to NPM, the ancient configuration file format is also used to describe the configuration files used for Rust projects. The ‘[dependencies]’ block is of special interest for us – it contains a list of all external libraries, which need to be present for the compilation to succeed.
Add A packet!
Creating a web server by hand is an ungrateful and annoying job best left to masochists. We will, instead, opt for a framework. Sadly, finding the right one is not easy – as shown in the picture above, visiting arewewebyet. org/topics/frameworks/ reveals an excessively long list of candidates.
We will pick Rocket, if only because it seems to be quite popular and has seen an update released in January of 2018. Sadly, Rocket’s developers tend to take a liking to newly-introduced language features, which is why frequent updating of your Rust installation using the following commands is required:
tamhan@tamhan-thinkpad:~/ rustspace/futuresample1$ rustup update && cargo update
The next step largely is a question of taste. Most libraries come with pre-provisioned starter projects, which developers can simply siphon from Github. Doing so for your ‘prime’ library is not necessarily a bad idea – although once more than one library is involved, a manual approach tends to be more fruitful.
Next, open the .toml file, then modify the ‘dependencies’ section as per the following in order to include a recent version of the Rocket framework:
[dependencies] rocket = "0.3.6" rocket_codegen = "0.3.6"
Rocket is unique in that it requires the inclusion of a total of two packages: in addition to the main framework, a separate code generator file is also required. Either way, our version includes a specific version of the two libraries – Cargo can also accept wildcards, which enable the program to ‘pick its poison’ without any help from us.
With that out of the way, one problem remains: enter ‘cargo run’ in the folder containing the .toml file in order to perform an assisted compile, which will – among other things – download the relevant code libraries from the repository and compile the whole enchilada for you.
Similarities to NPM are purely coincidential...