{"id":16467,"date":"2021-04-14T14:00:21","date_gmt":"2021-04-14T14:00:21","guid":{"rendered":"https:\/\/training.linuxfoundation.org\/?p=16467"},"modified":"2021-05-13T21:25:01","modified_gmt":"2021-05-13T21:25:01","slug":"using-web-assembly-written-in-rust-on-the-server-side","status":"publish","type":"post","link":"https:\/\/training.linuxfoundation.org\/ja\/blog\/using-web-assembly-written-in-rust-on-the-server-side\/","title":{"rendered":"\u30b5\u30fc\u30d0\u30fc\u5074\u3067Rust\u3067\u8a18\u8ff0\u3055\u308c\u305fWeb\u30a2\u30bb\u30f3\u30d6\u30ea\u3092\u4f7f\u7528\u3059\u308b"},"content":{"rendered":"<p style=\"text-align: center;\"><em>By Bob Reselman<\/em><\/p>\n<p><em>This article was originally published at <a href=\"https:\/\/thenewstack.io\/using-web-assembly-written-in-rust-on-the-server-side\/\">TheNewStac<\/a><\/em><em>k<\/em><\/p>\n<p>WebAssembly allows you to write code in a low-level programming language such as Rust, that gets compiled into a transportable binary. That binary can then be run on the client-side in the WebAssembly virtual machine that is\u00a0<a class=\"ext-link\" href=\"https:\/\/www.infoq.com\/news\/2017\/12\/webassembly-browser-support\/\" rel=\"external \">standard in today\u2019s web browsers<\/a>. Or, the binary can be used on the server-side, as a component consumed by another programming framework \u2014 such as Node.js or\u00a0<a class=\"ext-link\" href=\"https:\/\/deno.land\/\" rel=\"external \">Deno<\/a>.<\/p>\n<p>WebAssembly combines the efficiency inherent in low-level code programming with the ease of component transportability typically found in Linux containers. The result is a development paradigm specifically geared toward doing computationally intensive work at scale \u2014 for example, artificial intelligence and complex machine learning tasks.<\/p>\n<p>As Solomon Hykes, the creator of Docker,\u00a0<a class=\"ext-link\" href=\"https:\/\/twitter.com\/solomonstre\/status\/1111004913222324225?s=20\" rel=\"external \">tweeted<\/a> on March 27, 2019: \u201cIf WASM+WASI existed in 2008, we wouldn\u2019t have needed to have created Docker. That\u2019s how important it is. WebAssembly on the server is the future of computing.\u201d<\/p>\n<p>WebAssembly is a compelling approach to software development. However, in order to get a true appreciation for the technology, you need to see it in action.<\/p>\n<p>In this article, I am going to show you how to program a WebAssembly binary in Rust and use it in a TypeScript-powered web server running under Deno. I\u2019ll show you how to install Rust and prep the runtime environment. We\u2019ll compile the source code into a Rust binary. Then, once the binary is created, I\u2019ll demonstrate how to run it on the server-side under\u00a0<a class=\"ext-link\" href=\"https:\/\/deno.land\/\" rel=\"external \">Deno<\/a>. Deno is a TypeScript-based programming framework that was started by Ryan Dahl, the creator of Node.js.<\/p>\n<h2>Understanding the Demonstration Project<\/h2>\n<p>The demonstration project that accompanies this article is called Wise Sayings. The project stores a collection of \u201cwise sayings\u201d in a text file named wisesayings.txt. Each line in the text file is a wise saying, for example, \u201c<em>A friend in need is a friend indeed.<\/em>\u201d<\/p>\n<p>The Rust code publishes a single function, get_wise_saying(). That function gets a random line from the text file, wisesayings.txt, and returns the random line to the caller. (See Figure 1, below)<\/p>\n<div id=\"attachment_15520187\" class=\"wp-caption alignnone\">\n<p><a class=\"local-link\" href=\"https:\/\/cdn.thenewstack.io\/media\/2021\/03\/50e3e12f-image1.png\" rel=\"prettyPhoto[gallery-VMhd]\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15520187\" src=\"https:\/\/cdn.thenewstack.io\/media\/2021\/03\/50e3e12f-image1.png\" alt=\"\" width=\"571\" height=\"471\" aria-describedby=\"caption-attachment-15520187\" \/><\/a><\/p>\n<p id=\"caption-attachment-15520187\" class=\"wp-caption-text\">Figure 1: The demonstration project compiles data in a text file directly into the WebAssembly binary<\/p>\n<\/div>\n<p>Both the code and text file are compiled into a single WebAssembly binary file, named wisesayings.wasm. Then another layer of processing is performed to make the WebAssembly binary consumable by the Deno web server code. The Deno code calls the function get_wise_sayings() in the WebAssembly binary, to produce a random wise saying. (See Figure 2.)<\/p>\n<div id=\"attachment_15520205\" class=\"wp-caption alignnone\">\n<p><a class=\"local-link\" href=\"https:\/\/cdn.thenewstack.io\/media\/2021\/03\/ed3a0b83-image2.png\" rel=\"prettyPhoto[gallery-VMhd]\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-15520205\" src=\"https:\/\/cdn.thenewstack.io\/media\/2021\/03\/ed3a0b83-image2.png\" alt=\"\" width=\"721\" height=\"185\" aria-describedby=\"caption-attachment-15520205\" \/><\/a><\/p>\n<p id=\"caption-attachment-15520205\" class=\"wp-caption-text\">Figure 2: WebAssembly binaries can be consumed by a server-side programming framework such as Deno.<\/p>\n<\/div>\n<p><em>You get the source code for the Wise Sayings demonstration project used in this article\u00a0<a class=\"ext-link\" href=\"https:\/\/github.com\/reselbob\/wisesayingswasm\" rel=\"external \">on GitHub<\/a>. All the steps described in this article are listed on the repository\u2019s main\u00a0<a class=\"ext-link\" href=\"https:\/\/github.com\/reselbob\/wisesayingswasm\/blob\/main\/README.md\" rel=\"external \">Readme<\/a>\u00a0document.<\/em><\/p>\n<h2>Prepping the Development Environment<\/h2>\n<p>The first thing we need to do to get the code up and running is to make sure that Rust is installed in the development environment. The following steps describe the process.<\/p>\n<p><strong>Step 1:\u00a0<\/strong>Make sure Rust is installed on your machine by typing:<\/p>\n<div id=\"crayon-606def99b9f6d830245982\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\"><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16565\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-300x15.png\" alt=\"\" width=\"440\" height=\"22\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-300x15.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-600x31.png 600w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1.png 626w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/p>\n<p>You\u2019ll get output similar to the following:<\/p>\n<div id=\"crayon-606def99b9f74488139708\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\"><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16566\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/2-300x14.png\" alt=\"\" width=\"450\" height=\"21\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/2-300x14.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/2-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/2-600x29.png 600w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/2.png 626w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/p>\n<p>If the call to rustc \u2013version fails, you don\u2019t have Rust installed. Follow the instructions below and<strong>\u00a0make sure you do all the tasks presented by the given installation method<\/strong>.<\/p>\n<p>To install Rust, go here and install on Linux\/MAC: \u2026<\/p>\n<div id=\"crayon-606def99b9f76719251414\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-plain-wrap\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16567\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/3-300x15.png\" alt=\"\" width=\"440\" height=\"22\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/3-300x15.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/3-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/3-600x31.png 600w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/3.png 625w\" sizes=\"auto, (max-width: 440px) 100vw, 440px\" \/><\/div>\n<\/div>\n<p>\u2026 or here to install it on Windows:<\/p>\n<p>Download and run rustup-init.exe which you can find at this URL: https:\/\/static.rust-lang.org\/rustup\/dist\/i686-pc-windows-gnu\/rustup-init.exe.<\/p>\n<p><strong>Step 2:<\/strong>\u00a0Modify your system\u2019s PATH<\/p>\n<div id=\"crayon-606def99b9f77521973569\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16568\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/4-300x13.png\" alt=\"\" width=\"438\" height=\"19\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/4-300x13.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/4-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/4-600x27.png 600w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/4.png 627w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/div>\n<\/div>\n<p><strong>Step 3:\u00a0<\/strong>If you\u2019re working in a Linux environment do the following steps to install the required additional Linux components.<\/p>\n<div id=\"crayon-606def99b9f79878748983\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16569\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/5-300x52.png\" alt=\"\" width=\"433\" height=\"75\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/5-300x52.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/5-16x3.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/5.png 629w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/div>\n<\/div>\n<p><em><strong>Developer\u2019s Note:\u00a0<\/strong>The optimal development environment in which to run this code is one that uses the Linux operating system.<\/em><\/p>\n<p><strong>Step 4:\u00a0<\/strong>Get the CLI tool that you\u2019ll use for generating the TypeScript\/JavaScript adapter files. These adapter files (a.k.a. shims) do the work of exposing the function get_wise_saying() in the WebAssembly binary to the Deno web server that will be hosting the binary. Execute the following command at the command line to install the tool,\u00a0<a class=\"ext-link\" href=\"https:\/\/rustwasm.github.io\/docs\/wasm-bindgen\/reference\/cli.html\" rel=\"external \">wasm-bindgen-cli<\/a>.<\/p>\n<div id=\"crayon-606def99b9f7a229158948\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16570\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/6-300x13.png\" alt=\"\" width=\"438\" height=\"19\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/6-300x13.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/6-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/6-600x28.png 600w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/6.png 627w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/div>\n<\/div>\n<p>The development environment now has Rust installed, along with the necessary ancillary libraries. Now we need to get the Wise Saying source code.<\/p>\n<h2>Working with the Project Files<\/h2>\n<p>The Wise Saying source code is hosted in a GitHub repository. Take the following steps to clone the source code from GitHub onto the local development environment.<\/p>\n<p><strong>Step 1:\u00a0<\/strong>Execute the following command to clone the Wise Sayings source code from GitHub<\/p>\n<div id=\"crayon-606def99b9f7c065097142\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16571\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/7-300x14.png\" alt=\"\" width=\"450\" height=\"21\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/7-300x14.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/7-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/7.png 629w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><\/div>\n<\/div>\n<p><strong>Step 2:\u00a0<\/strong>Go to the working directory<\/p>\n<div id=\"crayon-606def99b9f7d862202746\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16572\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/8-300x15.png\" alt=\"\" width=\"460\" height=\"23\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/8-300x15.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/8-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/8.png 628w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><\/div>\n<\/div>\n<p>Listing 1, below lists the files that make up the source code cloned from the GitHub repository.<\/p>\n<div id=\"crayon-606def99b9f7f575022152\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16573\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/9-300x139.png\" alt=\"\" width=\"458\" height=\"212\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/9-300x139.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/9-16x7.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/9.png 630w\" sizes=\"auto, (max-width: 458px) 100vw, 458px\" \/><\/div>\n<\/div>\n<p><em>Listing 1: The files for the source code for the Wise Sayings demonstration project hosted in the GitHub repository<\/em><\/p>\n<p>Let\u2019s take a moment to describe the source code files listed above in Listing 1. The particular files of interest with regard to creating the WebAssembly binary are the files in the directory named, src at Line 11 and the file, Cargo.toml at Line 2.<\/p>\n<p>Let\u2019s discuss Cargo.toml first. The content of Cargo.toml is shown in Listing 2, below.<\/p>\n<div id=\"crayon-606def99b9f80348039243\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16574\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/10-300x139.png\" alt=\"\" width=\"453\" height=\"210\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/10-300x139.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/10-16x7.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/10.png 628w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/div>\n<\/div>\n<p><em>Listing 2: The content of Cargo.toml for the demonstration project Wise Sayings<\/em><\/p>\n<p>Cargo.toml is the\u00a0<a class=\"ext-link\" href=\"https:\/\/doc.rust-lang.org\/cargo\/reference\/manifest.html\" rel=\"external \">manifest file<\/a>\u00a0that describes various aspects of the Rust project under development. The Cargo.toml file for the Wise Saying project is organized into three sections: package, dependencies, and lib. The section names are defined in the Cargo manifest specification, which you read\u00a0<a class=\"ext-link\" href=\"https:\/\/doc.rust-lang.org\/cargo\/reference\/manifest.html\" rel=\"external \">here<\/a>.<\/p>\n<h3>Understanding the Package Section of Cargo.toml<\/h3>\n<p>The package section indicates the name of the package (wise-sayings-wasm), the developer assigned version (0.1.0), the authors (Bob Reselman &lt;<a href=\"mailto:bob@CogArtTech.com\">bob@CogArtTech.com<\/a>&gt;) and the edition of Rust (2018) that is used to program the binary.<\/p>\n<h3>Understanding the Dependencies Section of Cargo.toml<\/h3>\n<p>The dependencies section lists the dependencies that the WebAssembly project needs to do its work. As you can see in Listing 2, above at Line 8, the Cargo.toml lists the rand library as a dependency. The rand library provides the capability to generate a random number which is used to get a random line of wise saying text from the file, wisesayings.txt.<\/p>\n<p>The reference to getrandom at Line 9 in Listing 2 above indicates that the WebAssembly binary\u2019s\u00a0<a class=\"ext-link\" href=\"https:\/\/docs.rs\/getrandom\/0.2.2\/getrandom\/\" rel=\"external \">getrandom<\/a>\u00a0is running under Javascript and that the\u00a0<a class=\"ext-link\" href=\"https:\/\/docs.rs\/getrandom\/0.2.2\/getrandom\/#webassembly-support\" rel=\"external \">JavaScript interface should be used<\/a>. This condition is very particular to running a WebAssembly binary under JavaScript. The long and short of it is that if the line getrandom = { version = \u201c0.2\u201d, features = [\u201cjs\u201d] } is not included in the Cargo.toml, the WebAssembly binary will not be able to create a random number.<\/p>\n<p>The entry at Line 10 declares the\u00a0<a class=\"ext-link\" href=\"https:\/\/rustwasm.github.io\/docs\/wasm-bindgen\/\" rel=\"external \">wasm-bindgen<\/a>\u00a0library as a dependency. The wasm-bindgen library provides the capability for wasm modules to talk to JavaScript and JavaScript to talk to wasm modules.<\/p>\n<h3>Understanding the Lib Section of Cargo.toml<\/h3>\n<p>The entry\u00a0<a class=\"ext-link\" href=\"https:\/\/rustwasm.github.io\/docs\/wasm-pack\/tutorials\/npm-browser-packages\/template-deep-dive\/cargo-toml.html#1-crate-type\" rel=\"external \">crate-type =[\u201ccdylib\u201d, \u201clib\u201d]<\/a>\u00a0at Line 14 in the lib section of the Cargo.toml file tells the Rust compiler to create a wasm binary without a start function. Typically when cdylib is indicated, the compiler will create a\u00a0<a class=\"ext-link\" href=\"https:\/\/en.wikipedia.org\/wiki\/Library_(computing)#Shared_libraries\" rel=\"external \">dynamic library<\/a>\u00a0with the extension .dll in Windows, .so in Linux, or .dylib in MacOS. In this case, because the deployment unit is a WebAssembly binary, the compiler will create a file with the extension .wasm. The name of the wasm file will be wisesayings.wasm, as indicated at Line 13 above in Listing 2.<\/p>\n<p>The important thing to understand about Cargo.toml is that it provides both the design and runtime information needed to get your Rust code up and running. If the Cargo.toml file is not present, the Rust compiler doesn\u2019t know what to do and the build will fail.<\/p>\n<h2>Understanding the Core Function, get_wise_saying()<\/h2>\n<p>The actual work of getting a random line that contains a Wise Saying from the text file wisesayings.txt is done by the function get_wise_saying(). The code for get_wise_sayings() is in the Rust library file, .\/src\/lib.rs. The Rust code is shown below in Listing 3.<\/p>\n<div id=\"crayon-606def99b9f82182254100\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16575\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/11-300x129.png\" alt=\"\" width=\"482\" height=\"207\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/11-300x129.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/11-16x7.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/11.png 633w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><\/div>\n<\/div>\n<p><em>Listing 3: The function file, lib.rs contains the function, get_wise_saying().<\/em><\/p>\n<p>The important things to know about the source is that it\u2019s tagged at Line 4 with the attribute #[wasm_bindgen], which lets the Rust compiler know that the source code is targeted as a WebAssembly binary. The code publishes one function, get_wise_saying(), at Line 5. The way the wise sayings text file is loaded into memory is to use the\u00a0<a class=\"ext-link\" href=\"https:\/\/doc.rust-lang.org\/book\/ch19-06-macros.html\" rel=\"external \">Rust macro<\/a>,\u00a0<a class=\"ext-link\" href=\"https:\/\/doc.rust-lang.org\/std\/macro.include_str.html\" rel=\"external \">include_str!<\/a>. This macro does the work of getting the file from disk and loading the data into memory. The macro loads the file as a string and the function str.lines() separates the lines within the string into an array. (Line 7.)<\/p>\n<p>The rand::thread_rng() call at Line 10 returns a number that is used as an index by the .choose() function at Line 10. The result of it all is an array of characters (a string) that reflects the wise saying returned by the function.<\/p>\n<h2>Creating the WebAssembly Binary<\/h2>\n<p>Let\u2019s move on to creating the Wasm target and compiling the code into a WebAssembly Binary.<\/p>\n<p><strong>Step 1:<\/strong> Add the binary target.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-16947\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-1-300x13.png\" alt=\"\" width=\"485\" height=\"21\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-1-300x13.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-1-768x34.png 768w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-1-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-1-900x41.png 900w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/1-1.png 936w\" sizes=\"auto, (max-width: 485px) 100vw, 485px\" \/><\/strong><\/p>\n<p><strong>Step 2: <\/strong>Compile the source code into a WebAssembly is shown below.<\/p>\n<div id=\"crayon-606def99b9f85954461213\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16576\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/12-300x15.png\" alt=\"\" width=\"480\" height=\"24\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/12-300x15.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/12-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/12-600x31.png 600w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/12.png 627w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/div>\n<\/div>\n<p>WHERE<\/p>\n<ul>\n<li><strong>cargo build<\/strong>\u00a0is the command to invoke the compiler<\/li>\n<li><strong>\u2013lib<\/strong>\u00a0indicates that you\u2019re going to build a library<\/li>\n<li><strong>\u2013target<\/strong>\u00a0is the option that indicates target information<\/li>\n<li><strong>wasm-unknown-unknown<\/strong>\u00a0is the name of the target as well as the name of directory that will be created for the target\u2019s assets<\/li>\n<\/ul>\n<h3><strong>Understanding the Rust Target Triple Naming Convention<\/strong><\/h3>\n<p>Rust has a naming convention for targets. The term used for the convention is a\u00a0<em>target triple<\/em>. A target triple uses the following format: ARCH-VENDOR-SYS-ABI.<\/p>\n<p><strong>WHERE<\/strong><\/p>\n<p><strong>ARCH<\/strong>\u00a0describes the intended target architecture, for example wasm32 for WebAssembly, or i686 for current-generation Intel chips.<\/p>\n<p><strong>VENDOR<\/strong>\u00a0describes the vendor publishing the target; for example, Apple or Nvidia.<\/p>\n<p><strong>SYS<\/strong>\u00a0describes the operating system; for example, Windows or Linux.<\/p>\n<p><strong>ABI<\/strong>\u00a0describes how the process starts up, for eabi is used for bare metal, while gnu is used for glibc.<\/p>\n<p>Thus, the name i686-unknown-linux-gnu means that the Rust binary is targeted to an i686 architecture, the vendor is defined as unknown, the targeted operating system is Linux, and ABI is gnu.<\/p>\n<p>In the case of wasm32-unknown-unknown, the target is WebAssembly, the operating system is unknown and the ABI is unknown. The informal inference of the name is \u201cit\u2019s a WebAssembly binary.\u201d<\/p>\n<p>There are a standard set of built-in targets defined by Rust that can be found\u00a0<a class=\"ext-link\" href=\"https:\/\/docs.rust-embedded.org\/embedonomicon\/compiler-support.html#built-in-target\" rel=\"external \">here<\/a>.<\/p>\n<p>If you find the naming convention to be confusing because there are optional fields and sometimes there are four sections to the name, while other times there will be three sections, you are not alone.<\/p>\n<h2>Deploying the Binary Server-Side Using Deno<\/h2>\n<p>After we build the base WeAssembly binary, we need to create the adapter (a.k.a shim) files and a special version of the WebAssembly binary \u2014 all of which can be run from within JavaScript. We\u2019ll create these artifacts using the\u00a0<a class=\"ext-link\" href=\"https:\/\/rustwasm.github.io\/wasm-bindgen\/\" rel=\"external \">wasm-bindgen<\/a>\u00a0tool.<\/p>\n<p><strong>Step 1:\u00a0<\/strong>We create these new artifacts using the command shown below.<\/p>\n<div id=\"crayon-606def99b9f87161954894\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16577\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/13-300x16.png\" alt=\"\" width=\"469\" height=\"25\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/13-300x16.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/13-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/13.png 629w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/div>\n<\/div>\n<p>WHERE<\/p>\n<p><strong>wasm-bindgen<\/strong>\u00a0is the command for creating the adapter files and the special WebAssembly binary.<\/p>\n<p><strong>\u2013target deno .\/target\/wasm32-unknown-unknown\/debug\/wisesayings.wasm<\/strong>\u00a0is the option that indicates the adapter files will be targeted for Deno. Also, the option denotes the location of the original WebAssembly wasm binary that is the basis for the artifact generation process.<\/p>\n<p><strong>\u2013out-dir .\/server<\/strong>\u00a0is the option that declares the location where the created adapter files will be stored on disk; in this case,\u00a0<strong>.\/server<\/strong>.<\/p>\n<p>The result of running wasm-bindgen is the server directory shown in Listing 4 below.<\/p>\n<div id=\"crayon-606def99b9f89683045009\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16578\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/14-300x178.png\" alt=\"\" width=\"480\" height=\"285\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/14-300x178.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/14-16x9.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/14.png 629w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/div>\n<\/div>\n<p><em>Listing 4: The server directory contains the results of running wasm-bindgen<\/em><\/p>\n<p>Notice that the contents of the server directory, shown above in Listing 4, now has some added JavaScript (js) and TypeScript (ts) files. Also, the server directory has the special version of the WebAssembly binary, named wisesayings_bg.wasm. This version of the WebAssembly binary is a stripped-down version of the wasm file originally created by the initial compilation, done when invoking cargo build earlier. You can think of this new wasm file as a JavaScript-friendly version of the original WebAssembly binary. The suffix, _bg, is an abbreviation for bindgen.<\/p>\n<h2>Running the Deno Server<\/h2>\n<p>Once all the artifacts for running WebAssembly have been generated into the server directory, we\u2019re ready to invoke the Deno web server. Listing 5 below shows content of main.ts, which is the source code for the Deno web server.<\/p>\n<div id=\"crayon-606def99b9f8b280943498\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16579\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/15-300x171.png\" alt=\"\" width=\"479\" height=\"273\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/15-300x171.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/15-16x9.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/15.png 628w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/div>\n<\/div>\n<p><em>Listing 5: main.ts is the Deno webserver code that uses the WebAssembly binary<\/em><\/p>\n<p>You\u2019ll notice that the WebAssembly wasm binary is not imported directly. This is because the work of representing the WebAssembly binary is done by the JavaScript and TypeScript adapter (a.k.a shim) files generated earlier. The WebAssembly\/Rust function, get_wise_sayings(), is exposed in the auto-generated JavaScript file, wisesayings.js.<\/p>\n<p>The function get_wise_saying is imported into the webserver code at Line 2 above. The function is used at Line 16 to get a wise saying that will be returned as an HTTP response by the webserver.<\/p>\n<p>To get the Deno web server up and running, execute the following command in a terminal window.<\/p>\n<p><strong>Step 1:<\/strong><\/p>\n<div id=\"crayon-606def99b9f8d507131888\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16580\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/16-300x15.png\" alt=\"\" width=\"480\" height=\"24\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/16-300x15.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/16-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/16.png 630w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/div>\n<\/div>\n<p>WHERE<\/p>\n<p>deno run is the command set to invoke the webserver.<\/p>\n<p>\u2013allow-read is the option that allows the Deno webserver code to have permission to read files from disk.<\/p>\n<p>\u2013allow-net is the option that allows the Deno webserver code to have access to the network.<\/p>\n<p>\u2013allow-env is the option that allows the Deno webserver code read environment variables.<\/p>\n<p>.\/main.ts is the TypeScript file that Deno is to run. In this case, it\u2019s the webserver code.<\/p>\n<p>When the webserver is up and running, you\u2019ll get output similar to the following:<\/p>\n<p>HTTP webserver running at Thu Mar 11 2021 17:57:32 GMT+0000 (Coordinated Universal Time). Access it at: http:\/\/localhost:4040\/<\/p>\n<p><strong>Step 2:<\/strong><\/p>\n<p>Run the following command in a terminal on your computer to exercise the Deno\/WebAssembly code<\/p>\n<div id=\"crayon-606def99b9f8f668500776\" class=\"crayon-syntax crayon-theme-github-gist crayon-font-liberation-mono crayon-os-pc print-yes notranslate\" data-settings=\" touchscreen minimize scroll-mouseover\">\n<div class=\"crayon-main\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-16581\" src=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/17-300x15.png\" alt=\"\" width=\"480\" height=\"24\" srcset=\"https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/17-300x15.png 300w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/17-16x1.png 16w, https:\/\/training.linuxfoundation.org\/wp-content\/uploads\/2021\/04\/17.png 632w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/div>\n<\/div>\n<p>You\u2019ll get a wise saying, for example:<\/p>\n<p><em>True beauty lies within.<\/em><\/p>\n<p><strong>Congratulations!<\/strong>\u00a0You\u2019ve created and run a server-side WebAssembly binary.<\/p>\n<h2>Putting It All Together<\/h2>\n<p>In this article, I\u2019ve shown you everything you need to know to create and use a WebAssembly binary in a Deno web server. Yet for as detailed as the information presented in this article is, there is still a lot more to learn about what\u2019s under the covers. Remember, Rust is a low-level programming language. It\u2019s meant to go right up against the processor and memory directly. That\u2019s where its power really is. The real benefit of WebAssembly is using the technology to do computationally intensive work from within a browser. Applications that are well suited to WebAssembly are visually intensive games and activities that require complex machine learning capabilities \u2014 for example, real-time voice recognition and language translation. WebAssembly allows you to do computation on the client-side that previously was only possible on the server-side. As Solomon Hykes said, WebAssembly is the future of computing. He might very well be right.<\/p>\n<p>The important thing to understand is that WebAssembly provides enormous opportunities for those wanting to explore cutting-edge approaches to modern distributed computing. Hopefully, the information presented in this piece will motivate you to explore those opportunities.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>BobReselman\u8457\u3053\u306e\u8a18\u4e8b\u306f\u3082\u3068\u3082\u3068TheNewStackWebAssembly\u3067\u516c\u958b\u3055\u308c\u305f\u3082\u306e\u3067\u3001Rust\u306a\u3069\u306e\u4f4e\u6c34\u6e96\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u3067\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3057\u3066\u30c8\u30e9\u30f3\u30b9\u30dd\u30fc\u30bf\u30d6\u30eb\u306b\u30b3\u30f3\u30d1\u30a4\u30eb\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u2026<\/p>","protected":false},"author":92,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[78],"tags":[],"class_list":["post-16467","post","type-post","status-publish","format-standard","category-announcements"],"acf":[],"_links":{"self":[{"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/posts\/16467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/users\/92"}],"replies":[{"embeddable":true,"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/comments?post=16467"}],"version-history":[{"count":5,"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/posts\/16467\/revisions"}],"predecessor-version":[{"id":16948,"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/posts\/16467\/revisions\/16948"}],"wp:attachment":[{"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/media?parent=16467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/categories?post=16467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/training.linuxfoundation.org\/ja\/wp-json\/wp\/v2\/tags?post=16467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}