{"id":56260,"date":"2022-06-28T03:01:11","date_gmt":"2022-06-27T17:01:11","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=56260"},"modified":"2022-07-06T15:54:49","modified_gmt":"2022-07-06T05:54:49","slug":"webassembly-rust-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/webassembly-rust-primer-tutorial\/","title":{"rendered":"WebAssembly Rust Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/webassembly_rust.gif\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"WebAssembly Rust Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/webassembly_rust.gif\" title=\"WebAssembly Rust Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">WebAssembly Rust Primer Tutorial<\/p><\/div>\n<p>Today, we&#8217;re closely following the tutorial at <a target=_blank title='Compiling from Rust to WebAssembly' href='https:\/\/developer.mozilla.org\/en-US\/docs\/WebAssembly\/Rust_to_wasm'>Compiling from Rust to WebAssembly<\/a>, thanks, to show that, yes, as well as C (and\/or C++), as shown in yesterday&#8217;s <a title='WebAssembly via Emscripten and C Primer Tutorial' href='#waecpt'>WebAssembly via Emscripten and C Primer Tutorial<\/a> you can create a Web Assembly in the Rust programming language.<\/p>\n<p>We show, for a &#8220;Hello, WebAssembly!&#8221; proof of concept scenario that revisits many of the same themes as our previous <a target=_blank href='https:\/\/www.rjmprogramming.com.au\/ITblog\/rust-primer-tutorial\/' title='Rust Primer Tutorial'>Rust Primer Tutorial<\/a> &#8220;delving into Rust&#8221; &#8230;<\/p>\n<ol>\n<li>Reinstalling Rust on this macOS MacBook Air we hadn&#8217;t installed to in the past &#8230;<\/li>\n<li>Building WebAssembly package &#8230;<\/li>\n<li>Using wasm-bindgen to communicate between Rust and JavaScript &#8230;<\/li>\n<li>Calling external functions in JavaScript from Rust &#8230;<\/li>\n<li>Producing Rust functions that JavaScript can call &#8230;<\/li>\n<li>Compiling of code to WebAssembly &#8230;<\/li>\n<li>Building package &#8230;<\/li>\n<li>Using the package on the web in our local <a target=_blank title='MAMP' href='http:\/\/www.mamp.info'>MAMP<\/a> environment <a target=_blank title='MAMP Rust WebAssembly Hello, WebAssembly! only if you are exactly finishing all this like for my macOS MAMP HTTP:\/\/localhost:8888\/hello-wasm\/ scenario' href='HTTP:\/\/localhost:8888\/hello-wasm\/'>here<\/a> <font size=1>(MAMP Rust WebAssembly Hello, WebAssembly! only if you are exactly finishing all this like for my macOS MAMP HTTP:\/\/localhost:8888\/hello-wasm\/ scenario)<\/font> &#8230;<\/li>\n<li>Using the package on the web in our public RJM Programming domain environment (after sftp of whole package contents) <font size=1>(temporarily, for you to see what is possible)<\/font><\/li>\n<\/ol>\n<p> &#8230; command line wise amounting to (where the vi session inserted code came from the tutorial exactly) &#8230;<\/p>\n<p><code><br \/>\n$ <a target=_blank title='Rust install on macOS' href='https:\/\/rust-lang.org\/tools\/install'>curl --proto '=https' --tlsv1.2 -sSf https:\/\/sh.rustup.rs | sh<\/a><br \/>\n$ cargo install wasm-pack<br \/>\n$ cargo new --lib hello-wasm<br \/>\n$ cd hello-wasm<br \/>\n$ vi src\/lib.rs<br \/>\n$ vi Cargo.toml<br \/>\n$ wasm-pack build --target web<br \/>\n$ vi <a target=_blank title='MAMP Rust WebAssembly Hello, WebAssembly! only if you are exactly finishing all this like for my macOS MAMP HTTP:\/\/localhost:8888\/hello-wasm\/ scenario' href='HTTP:\/\/localhost:8888\/hello-wasm\/'>index.html<\/a><br \/>\n<\/code><\/p>\n<p> &#8230; you can see with <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/webassembly_rust.gif\" title=\"Click picture\">today&#8217;s animated GIF<\/a> presentation we hope you enjoy!<\/p>\n<p><!--p>You can also see this play out at WordPress 4.1.1's <a target=_blank  href='\/\/www.rjmprogramming.com.au\/ITblog\/webassembly-rust-primer-tutorial\/'>WebAssembly Rust Primer Tutorial<\/a>.<\/p-->\n<hr>\n<p id='waecpt'>Previous relevant <a target=_blank title='WebAssembly via Emscripten and C Primer Tutorial' href='\/\/www.rjmprogramming.com.au\/ITblog\/webassembly-via-emscripten-and-c-primer-tutorial\/'>WebAssembly via Emscripten and C Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/fibonacci.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"WebAssembly via Emscripten and C Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/webassembly_c.gif\" title=\"WebAssembly via Emscripten and C Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">WebAssembly via Emscripten and C Primer Tutorial<\/p><\/div>\n<p>Have you done any web application work with a <a target=_blank title='WebAssembly Concepts ... thanks' href='https:\/\/developer.mozilla.org\/en-US\/docs\/WebAssembly\/Concepts'>WebAssembly<\/a>?  It gets you being able to bring your C or C++ or Rust code into a web application.  Great idea, huh?!  Today we&#8217;re presenting some simple usages of a WebAssembly made via two small pieces of C code &#8230;<\/p>\n<ul>\n<li>WebAssembly of <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/hello.html\" title=\"Click picture\">&#8220;Hello World&#8221;<\/a> via <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/hello.c_GETME\" title=\"hello.c\">hello.c<\/a> C code generating &#8230;\n<ol>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/hello.html_GETME\" title=\"hello.html\">hello.html<\/a><\/li>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/hello.js_GETME\" title=\"hello.js\">hello.js<\/a><\/li>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/hello.wasm_GETME\" title=\"hello.wasm\">hello.wasm<\/a><\/li>\n<p> &#8230; via Emscripten &#8230; thanks\n<\/ol>\n<p><iframe src=\"http:\/\/www.rjmprogramming.com.au\/Mac\/hello.html\" title=\"Click picture\" style=\"width:100%;height:500px;\"><\/iframe>\n<\/li>\n<li>WebAssembly of <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/fibonacci.html\" title=\"Click picture\">Fibonacci series<\/a> <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/fibonacci.html_GETME\" title=\"fibonacci.html\">fibonacci.html<\/a> via <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/main.c_GETME\" title=\"main.c\">main.c<\/a> C code generating &#8230;\n<ol>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/main.html_GETME\" title=\"main.html\">main.html<\/a><\/li>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/main.js_GETME\" title=\"main.js\">main.js<\/a><\/li>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Mac\/main.wasm_GETME\" title=\"main.wasm\">main.wasm<\/a><\/li>\n<p> &#8230; via Emscripten &#8230; thanks\n<\/ol>\n<p><input type=button onclick=\"document.getElementById('fibif').style.display='block'; document.getElementById('fibif').scrollIntoView(); \n document.getElementById('fibif').src='http:\/\/www.rjmprogramming.com.au\/Mac\/fibonacci.html'; \" value=\"Click for Emscripten Fibonacci series via C code\"><\/input><br \/>\n<iframe id=fibif src=\"http:\/\/www.rjmprogramming.com.au\/About_Us.html\" title=\"Click picture\" style=\"width:100%;height:900px;display:none;\"><\/iframe><span id=satend>&nbsp;<\/span>\n<\/li>\n<\/ul>\n<p>To get started, and going, here, we liked <a target=_blank title='Compiling a New C\/C++ Module to WebAssembly' href='https:\/\/developer.mozilla.org\/en-US\/docs\/WebAssembly\/C_to_wasm'>Compiling a New C\/C++ Module to WebAssembly<\/a> as far as advice goes and needed Emscripten download resources at <a target=_blank href='https:\/\/emscripten.org\/docs\/getting_started\/downloads.html' title='Emscripten Download and install'>Emscripten Download and install<\/a> and <a target=_blank title='CMake download and install' href='https:\/\/cmake.org\/download\/'>CMake<\/a> on our macOS local environment, allowing us to compile the html via a command such as &#8230;<\/p>\n<p><code><br \/>\nemcc hello.c -o hello.html<br \/>\n<\/code><\/p>\n<p> &#8230; allowing the sftp of the html and js and wasm over to the RJM Programming public web domain, and show you, in action, today.  Cute, huh, given the code is C by nature and inclination!<\/p>\n<p>Word of warning with the Fibonacci code is that use of C <a target=_blank title='C scanf function information' href='https:\/\/www.tutorialspoint.com\/c_standard_library\/c_function_scanf.htm'><i>scanf<\/i><\/a> function is a little sketchy in its behaviour, hence our wrapping of the Emscripten html within an iframe, where topmost parent html explains the issue, a bit.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d56244' onclick='var dv=document.getElementById(\"d56244\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/compile\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d56244' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d56260' onclick='var dv=document.getElementById(\"d56260\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/package\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d56260' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today, we&#8217;re closely following the tutorial at Compiling from Rust to WebAssembly, thanks, to show that, yes, as well as C (and\/or C++), as shown in yesterday&#8217;s WebAssembly via Emscripten and C Primer Tutorial you can create a Web Assembly &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/webassembly-rust-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,37],"tags":[170,176,233,234,241,576,611,2178,3069,4022,4021,2976,997,999,1986,1081,1114,1166,1319,1402,4002],"class_list":["post-56260","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-build","tag-c","tag-command","tag-command-line","tag-compile","tag-html","tag-install","tag-macos","tag-package","tag-permission","tag-permissions","tag-privacy","tag-programming","tag-project","tag-proof-of-concept","tag-rust","tag-security","tag-software-2","tag-tutorial","tag-web-application","tag-webassembly"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56260"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=56260"}],"version-history":[{"count":9,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56260\/revisions"}],"predecessor-version":[{"id":56269,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56260\/revisions\/56269"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=56260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=56260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=56260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}