2019-05-01_Linux_Format

(singke) #1

92 LXF249 May 2019 http://www.linuxformat.com


CODING ACADEMY Speed up web apps


Rusttoolsareinstalledin~/.cargo/bin, soensurethat
~/.cargo/binisinyourPATHenvironmentvariable.
Theoutputoftherustc--versioncommandwillverify
thateverythingisOKwithyourRustinstallation.
If youtrytoexecute cargogeneratewithoutfirst
installingtherequiredRustcrate,you’llseethe
following error:
$ cargo generate
error: no such subcommand: `generate`
This happens because cargo generate is not a
standard command of the Cargo tool. Installing
WebAssembly support for Rust is the most difficult and
challenging task you will find in this tutorial; at the end
of it, we’ll see that this isn’t the case with WebAssembly
and Go, as WebAssembly is just another supported
architecture for Go.
In a moment we’ll look at a simple Rust project that
generates WebAssembly code, which is going to be
served using HTML and JavaScript. You will need to use
nmp (Node Package Manager) to be able to test the
generated applications on your local machine. As npm
is installed with Node.js, you need to install Node.js to
get npm on your computer. Therefore, it would be a
good idea to install Node.js at this point. For the Ubuntu
machine used in this tutorial, installing Node.js and npm
is as simple as executing apt install npm with root
privileges – this installs both components.

avaScripthasbeenthedominantprogramming
languageamongwebdevelopersfora long
timenow.WebAssemblywantstobecomethe
newkingofwebdevelopment– notasa programming
language,butasa virtualandportablecompilation
target.ThereforeWebAssemblyisnotheretoreplace
JavaScriptentirely,buttoworkwithJavaScript.The
twobiggestadvantagesofWebAssemblyareportability
andgoodperformance.
ThistutorialisaboutwritingRustcode,compilingit
intoWebAssemblyandusingthatWebAssemblycode.
Atthetimeofwriting,thelatestRustversionis1.32.0.
However,youshouldnothaveanyproblemsexecuting
thecodeofthisarticleevenwitholderRustversions.
Attheendofthetutorialwe’llseehowtocreate
WebAssemblycodefromGo,sowecancompare
theRustwaywiththeGoapproach.

Installation
Asalwayswe’llneedtoinstallWebAssemblyonour
Linuxmachineinordertouseit withRust.First,you
willneedtoinstallthestandardRusttoolchain,which
includesrustc, theRustcompiler;rustup, theRust
toolchaininstaller;andcargo, whichisthepackage
managerforRust.Theeasiestwaytoinstallallthese
istofollowtheinstructionsatwww.rust-lang.org/
tools/install. If youprefer,youcanuseyourfavourite
Linuxpackagemanagerinstead.
AfterinstallingtheRusttoolchain,youshould install
wasm-packbyfollowingtheinstructionsathttp://bit.
ly/lxf249rust2^1. Thenyouwillneedtoinstallcargo-
generatetomakecreatingWebAssemblyprojects in
Rustsimplerandeasier.Ascargo-generateisjust a
Rustcrate,it canbeinstalledbyexecuting cargo install
cargo-generate. Bepreparedfora lengthyinstallation
process;it includestheexecutionofthefollowing:
$ sudocurlhttps://sh.rustup.rs-sSf| sh
$ sudocurlhttps://rustwasm.github.io/wasm-pack/
installer/init.sh-sSf| sh
$ sudoaptinstalllibssl-devpkg-config
$ cargoinstallcargo-generate
Figure1 showsa smallpartoftheoutputofthe
commandsfromtheinstallationprocess.Notethat all

Figure1: A smallpartoftheinstallationprocessoftheRust
WebAssemblysupport,whichisa prettylengthytask.

Speed your web apps


with WebAssembly


Mihalis Tsoukalos explains how to develop WebAssembly applications


from Rust, and how to use them with the help of JavaScript.


Mihalis
Tsoukalos
is a UNIX person
and the author of
Go Systems
Programming
and Mastering
Go. You can
reach him at
http://www.mtsoukalos.
eu and @
mactsouk.


WEBASSEMBLY + RUST


OUR
EXPERT

Getthecodefor
thisprojecton
theLXFDVD
orvisitwww.
linuxformat.
com/archives
andgrabteh
codepack.

J


1) https://rustwasm.github.io/wasm-pack/installer/
Free download pdf