{"id":366182,"date":"2024-05-21T02:53:57","date_gmt":"2024-05-21T02:53:57","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=366182"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=366182","title":{"rendered":"<span>\u041a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f WebAssembly \u0432 Rust: \u043a\u0440\u0430\u0442\u043a\u043e<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f40\/b1d\/67e\/f40b1d67ee262c1ef75fa5400838a231.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f40\/b1d\/67e\/f40b1d67ee262c1ef75fa5400838a231.png\"\/><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p><strong>Rust<\/strong> &#8212; \u044f\u0437\u044b\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0433\u043e\u0434\u044b \u0437\u0430\u0432\u043e\u0435\u0432\u0430\u043b \u0441\u0435\u0440\u0434\u0446\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0441\u0432\u043e\u0435\u0439 \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0441\u0442\u044c\u044e, \u0430 \u0432 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 <a href=\"https:\/\/habr.com\/ru\/companies\/otus\/articles\/787362\/\">\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e<\/a>. <strong>WebAssembly <\/strong>\u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u043c\u043e\u0449\u044c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440. \u0412\u043c\u0435\u0441\u0442\u0435 \u044d\u0442\u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e, \u043d\u0443 \u0438 \u043a\u043e\u043d\u0447\u0435\u0447\u043d\u043e &#8212; \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u0437.\u044b: \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u0437\u043d\u0430\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u044b webassembly \u0438 \u043f\u0440\u043e \u0447\u0442\u043e-\u0442\u043e \u0438\u0437 rust<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<h4>wasm-bindgen<\/h4>\n<p><code>wasm-bindgen<\/code> \u2013 \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u043b\u0435\u0433\u0447\u0430\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u0434\u043e\u043c \u043d\u0430 Rust \u0438 JavaScript, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 JavaScript \u0438\u0437 Rust \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 JavaScript \u043f\u0440\u044f\u043c\u043e \u0438\u0437 Rust. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u0438 \u0432\u0435\u0431-API, \u043d\u0435 \u043f\u043e\u043a\u0438\u0434\u0430\u044f \u044d\u043a\u043e\u0441\u0438\u0441\u0442\u0435\u043c\u0443 Rust.<\/p>\n<p>wasm-bindgen \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Rust \u0432 WebAssembly, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0442\u0435\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u043d\u044b \u0438\u0437 JavaScript, \u0442\u0430\u043a\u0436\u0435 \u043e\u043d \u043e\u0431\u043b\u0435\u0433\u0447\u0430\u0435\u0442 \u043e\u0431\u043c\u0435\u043d \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 Rust \u0438 JavaScript, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0438 \u0434\u0430\u0436\u0435 \u0446\u0435\u043b\u044b\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f JavaScript, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c:<\/p>\n<pre><code class=\"javascript\">function greet(name) {     console.log(`Hello, ${name}!`); }<\/code><\/pre>\n<p>\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438\u0437 Rust \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] extern \"C\" {     fn greet(name: &amp;str); }  #[wasm_bindgen(start)] pub fn run() {     greet(\"World\"); }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430 Rust \u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u0435\u0451 \u0438\u0437 JavaScript:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] pub fn add(x: i32, y: i32) -> i32 {     x + y }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438\u0437 JavaScript:<\/p>\n<pre><code class=\"javascript\">import { add } from '.\/my_module';  console.log(add(5, 7)); \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u0442 12<\/code><\/pre>\n<p><code>wasm-bindgen<\/code> \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 Rust \u0432 JavaScript. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] pub struct Point {     x: i32,     y: i32, }  #[wasm_bindgen] impl Point {     pub fn new(x: i32, y: i32) -> Point {         Point { x, y }     }      pub fn x(&amp;self) -> i32 {         self.x     }      pub fn y(&amp;self) -> i32 {         self.y     } }<\/code><\/pre>\n<p>\u0412 JavaScript \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <code>Point<\/code> \u0442\u0430\u043a:<\/p>\n<pre><code class=\"javascript\">import { Point } from '.\/my_module';  const p = Point.new(2, 3); console.log(p.x()); \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u0442 2 console.log(p.y()); \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u0442 3<\/code><\/pre>\n<h4>wasm-pack<\/h4>\n<p><code>wasm-pack<\/code> \u2013 \u044d\u0442\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u043a\u0438 Rust-\u043a\u043e\u0434\u0430 \u0432 WebAssembly \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e \u043a\u043e\u043d\u0446\u0430. <\/p>\n<p><code>wasm-pack<\/code> \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442 Rust-\u043a\u043e\u0434 \u0432 WebAssembly, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044f \u0435\u0433\u043e \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0432\u0435\u0431\u0435. \u041f\u043e\u0441\u043b\u0435 \u0441\u0431\u043e\u0440\u043a\u0438 <code>wasm-pack<\/code> \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 \u0441 npm, \u0447\u0442\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<p><code>wasm-pack<\/code> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u043d\u0430 WebAssembly.<\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 <code>wasm-pack<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>cargo<\/code>:<\/p>\n<pre><code class=\"bash\">cargo install wasm-pack<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>cargo<\/code>:<\/p>\n<pre><code class=\"bash\">cargo new --lib my_wasm_project cd my_wasm_project<\/code><\/pre>\n<p>\u0412 \u0444\u0430\u0439\u043b\u0435 <code>Cargo.toml<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c<code>wasm-bindgen<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a <code>wasm-pack<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0442\u0430\u043d\u0434\u0435\u043c\u0435 \u0441 \u043d\u0438\u043c:<\/p>\n<pre><code>[lib] crate-type = [\"cdylib\"]  [dependencies] wasm-bindgen = \"0.2\"<\/code><\/pre>\n<p>\u0412 \u0444\u0430\u0439\u043b\u0435 <code>src\/lib.rs<\/code> \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] pub fn greet(name: &amp;str) -> String {     format!(\"Hello, {}!\", name) <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>wasm-pack<\/code>:<\/p>\n<pre><code class=\"bash\">wasm-pack build<\/code><\/pre>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u043f\u0430\u043f\u043a\u0443 <code>pkg<\/code>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 WebAssembly \u043a\u043e\u0434 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 JavaScript-\u043e\u0431\u0432\u044f\u0437\u043a\u0438.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0441\u0431\u043e\u0440\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043f\u0430\u043a\u0435\u0442 \u0432 npm:<\/p>\n<pre><code class=\"bash\">wasm-pack publish<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c WebAssembly-\u043c\u043e\u0434\u0443\u043b\u044c \u0433\u043e\u0442\u043e\u0432 \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0432 JavaScript-\u043f\u0440\u043e\u0435\u043a\u0442\u0435. \u041c\u043e\u0436\u043d\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>greet<\/code> \u0432 JavaScript-\u043a\u043e\u0434\u0435:<\/p>\n<pre><code class=\"javascript\">import { greet } from 'my_wasm_project';  console.log(greet('World')); \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u0442 \"Hello, World!\"<\/code><\/pre>\n<h4>cargo-web<\/h4>\n<p><code>cargo-web<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u0434\u0430.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c <code>cargo-web<\/code>:<\/p>\n<pre><code class=\"bash\">cargo install cargo-web<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442:<\/p>\n<pre><code class=\"bash\">cargo new --lib my_web_project cd my_web_project<\/code><\/pre>\n<p>\u0412 <code>Cargo.toml<\/code>, \u0443\u043a\u0430\u0436\u0435\u043c \u0442\u0438\u043f \u0441\u0431\u043e\u0440\u043a\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code>[package] name = \"my_project\" version = \"1\" edition = \"2\"  [lib] crate-type = [\"cdylib\"]  [dependencies] wasm-bindgen = \"0.2\"<\/code><\/pre>\n<p>\u0412 <code>src\/lib.rs<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] pub fn say_hello() -> String {     \"Hello from Rust!\".to_string() }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>cargo-web<\/code>:<\/p>\n<pre><code class=\"bash\">cargo web start<\/code><\/pre>\n<p>\u042d\u0442\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043e\u0442\u043a\u0440\u043e\u0435\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. \u041b\u044e\u0431\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u0434\u0435 \u0431\u0443\u0434\u0443\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>say_hello<\/code> \u0432 \u0432\u0435\u0431-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c HTML-\u0444\u0430\u0439\u043b:<\/p>\n<pre><code class=\"xml\">&lt;!DOCTYPE html> &lt;html> &lt;head>     &lt;title>Rust WebAssembly App&lt;\/title>     &lt;script type=\"module\">         import init, { say_hello } from '.\/my_web_project.js';          async function run() {             await init();             alert(say_hello());         }          run();     &lt;\/script> &lt;\/head> &lt;body>     &lt;h1>Hello from Rust and WebAssembly!&lt;\/h1> &lt;\/body> &lt;\/html><\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442 \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 WebAssembly-\u043c\u043e\u0434\u0443\u043b\u044c, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>say_hello<\/code>.<\/p>\n<hr\/>\n<p>Rust \u0438\u043c\u0435\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 WASM. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438<\/p>\n<p>\u0412 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0442\u0435\u043c\u044b \u0445\u043e\u0447\u0443 \u043d\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0435 \u0432\u0435\u0431\u0438\u043d\u0430\u0440\u044b \u043e\u0442 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432 \u0440\u044b\u043d\u043a\u0430 \u043f\u0440\u043e\u00a0<a href=\"https:\/\/otus.pw\/TUW2\/\">\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 unsafe Rust<\/a>\u00a0\u0438 \u043f\u0440\u043e \u0442\u043e,\u00a0<a href=\"https:\/\/otus.pw\/tR78\/\">\u043a\u0430\u043a Rust \u043f\u043e\u0431\u0443\u0436\u0434\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044e<\/a>.  <\/p>\n<p>\u0410 \u0431\u043e\u043b\u044c\u0448\u0435 \u043a\u0443\u0440\u0441\u043e\u0432 \u043e\u0442 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432 OTUS \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 <a href=\"https:\/\/otus.pw\/OGvV\/\">\u043f\u043e\u043b\u043d\u043e\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435<\/a>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/788602\/\"> https:\/\/habr.com\/ru\/articles\/788602\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p><em>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/em><\/p>\n<p><strong>Rust<\/strong> &#8212; \u044f\u0437\u044b\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0433\u043e\u0434\u044b \u0437\u0430\u0432\u043e\u0435\u0432\u0430\u043b \u0441\u0435\u0440\u0434\u0446\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0441\u0432\u043e\u0435\u0439 \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0441\u0442\u044c\u044e, \u0430 \u0432 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 <a href=\"https:\/\/habr.com\/ru\/companies\/otus\/articles\/787362\/\">\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e<\/a>. <strong>WebAssembly <\/strong>\u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u043c\u043e\u0449\u044c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440. \u0412\u043c\u0435\u0441\u0442\u0435 \u044d\u0442\u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e, \u043d\u0443 \u0438 \u043a\u043e\u043d\u0447\u0435\u0447\u043d\u043e &#8212; \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u0437.\u044b: \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u0437\u043d\u0430\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u044b webassembly \u0438 \u043f\u0440\u043e \u0447\u0442\u043e-\u0442\u043e \u0438\u0437 rust<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h2>\n<h4>wasm-bindgen<\/h4>\n<p><code>wasm-bindgen<\/code> \u2013 \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u043b\u0435\u0433\u0447\u0430\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u0434\u043e\u043c \u043d\u0430 Rust \u0438 JavaScript, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 JavaScript \u0438\u0437 Rust \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 JavaScript \u043f\u0440\u044f\u043c\u043e \u0438\u0437 Rust. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u0438 \u0432\u0435\u0431-API, \u043d\u0435 \u043f\u043e\u043a\u0438\u0434\u0430\u044f \u044d\u043a\u043e\u0441\u0438\u0441\u0442\u0435\u043c\u0443 Rust.<\/p>\n<p>wasm-bindgen \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Rust \u0432 WebAssembly, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0442\u0435\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u043d\u044b \u0438\u0437 JavaScript, \u0442\u0430\u043a\u0436\u0435 \u043e\u043d \u043e\u0431\u043b\u0435\u0433\u0447\u0430\u0435\u0442 \u043e\u0431\u043c\u0435\u043d \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 Rust \u0438 JavaScript, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0438 \u0434\u0430\u0436\u0435 \u0446\u0435\u043b\u044b\u043c\u0438 \u043a\u043b\u0430\u0441\u0441\u0430\u043c\u0438.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f JavaScript, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c:<\/p>\n<pre><code class=\"javascript\">function greet(name) {     console.log(`Hello, ${name}!`); }<\/code><\/pre>\n<p>\u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438\u0437 Rust \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] extern \"C\" {     fn greet(name: &amp;str); }  #[wasm_bindgen(start)] pub fn run() {     greet(\"World\"); }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430 Rust \u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u0435\u0451 \u0438\u0437 JavaScript:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] pub fn add(x: i32, y: i32) -> i32 {     x + y }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438\u0437 JavaScript:<\/p>\n<pre><code class=\"javascript\">import { add } from '.\/my_module';  console.log(add(5, 7)); \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u0442 12<\/code><\/pre>\n<p><code>wasm-bindgen<\/code> \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 Rust \u0432 JavaScript. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] pub struct Point {     x: i32,     y: i32, }  #[wasm_bindgen] impl Point {     pub fn new(x: i32, y: i32) -> Point {         Point { x, y }     }      pub fn x(&amp;self) -> i32 {         self.x     }      pub fn y(&amp;self) -> i32 {         self.y     } }<\/code><\/pre>\n<p>\u0412 JavaScript \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <code>Point<\/code> \u0442\u0430\u043a:<\/p>\n<pre><code class=\"javascript\">import { Point } from '.\/my_module';  const p = Point.new(2, 3); console.log(p.x()); \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u0442 2 console.log(p.y()); \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u0442 3<\/code><\/pre>\n<h4>wasm-pack<\/h4>\n<p><code>wasm-pack<\/code> \u2013 \u044d\u0442\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0439 \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u043a\u0438 Rust-\u043a\u043e\u0434\u0430 \u0432 WebAssembly \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u043c \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e \u043a\u043e\u043d\u0446\u0430. <\/p>\n<p><code>wasm-pack<\/code> \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442 Rust-\u043a\u043e\u0434 \u0432 WebAssembly, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044f \u0435\u0433\u043e \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0432\u0435\u0431\u0435. \u041f\u043e\u0441\u043b\u0435 \u0441\u0431\u043e\u0440\u043a\u0438 <code>wasm-pack<\/code> \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442, \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u044b\u0439 \u0441 npm, \u0447\u0442\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435.<\/p>\n<p><code>wasm-pack<\/code> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u043d\u0430 WebAssembly.<\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 <code>wasm-pack<\/code> \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>cargo<\/code>:<\/p>\n<pre><code class=\"bash\">cargo install wasm-pack<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>cargo<\/code>:<\/p>\n<pre><code class=\"bash\">cargo new --lib my_wasm_project cd my_wasm_project<\/code><\/pre>\n<p>\u0412 \u0444\u0430\u0439\u043b\u0435 <code>Cargo.toml<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c<code>wasm-bindgen<\/code>, \u0442\u0430\u043a \u043a\u0430\u043a <code>wasm-pack<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0442\u0430\u043d\u0434\u0435\u043c\u0435 \u0441 \u043d\u0438\u043c:<\/p>\n<pre><code>[lib] crate-type = [\"cdylib\"]  [dependencies] wasm-bindgen = \"0.2\"<\/code><\/pre>\n<p>\u0412 \u0444\u0430\u0439\u043b\u0435 <code>src\/lib.rs<\/code> \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] pub fn greet(name: &amp;str) -> String {     format!(\"Hello, {}!\", name) <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u0440\u043e\u0435\u043a\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>wasm-pack<\/code>:<\/p>\n<pre><code class=\"bash\">wasm-pack build<\/code><\/pre>\n<p>\u041a\u043e\u043c\u0430\u043d\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u043f\u0430\u043f\u043a\u0443 <code>pkg<\/code>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 WebAssembly \u043a\u043e\u0434 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 JavaScript-\u043e\u0431\u0432\u044f\u0437\u043a\u0438.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0441\u0431\u043e\u0440\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u043f\u0430\u043a\u0435\u0442 \u0432 npm:<\/p>\n<pre><code class=\"bash\">wasm-pack publish<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c WebAssembly-\u043c\u043e\u0434\u0443\u043b\u044c \u0433\u043e\u0442\u043e\u0432 \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0432 JavaScript-\u043f\u0440\u043e\u0435\u043a\u0442\u0435. \u041c\u043e\u0436\u043d\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>greet<\/code> \u0432 JavaScript-\u043a\u043e\u0434\u0435:<\/p>\n<pre><code class=\"javascript\">import { greet } from 'my_wasm_project';  console.log(greet('World')); \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u0442 \"Hello, World!\"<\/code><\/pre>\n<h4>cargo-web<\/h4>\n<p><code>cargo-web<\/code> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0432\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u043a\u043e\u0434\u0430.<\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c <code>cargo-web<\/code>:<\/p>\n<pre><code class=\"bash\">cargo install cargo-web<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442:<\/p>\n<pre><code class=\"bash\">cargo new --lib my_web_project cd my_web_project<\/code><\/pre>\n<p>\u0412 <code>Cargo.toml<\/code>, \u0443\u043a\u0430\u0436\u0435\u043c \u0442\u0438\u043f \u0441\u0431\u043e\u0440\u043a\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code>[package] name = \"my_project\" version = \"1\" edition = \"2\"  [lib] crate-type = [\"cdylib\"]  [dependencies] wasm-bindgen = \"0.2\"<\/code><\/pre>\n<p>\u0412 <code>src\/lib.rs<\/code> \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438:<\/p>\n<pre><code class=\"rust\">use wasm_bindgen::prelude::*;  #[wasm_bindgen] pub fn say_hello() -> String {     \"Hello from Rust!\".to_string() }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>cargo-web<\/code>:<\/p>\n<pre><code class=\"bash\">cargo web start<\/code><\/pre>\n<p>\u042d\u0442\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 \u0432\u0435\u0431-\u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043e\u0442\u043a\u0440\u043e\u0435\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. \u041b\u044e\u0431\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u0434\u0435 \u0431\u0443\u0434\u0443\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>say_hello<\/code> \u0432 \u0432\u0435\u0431-\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c HTML-\u0444\u0430\u0439\u043b:<\/p>\n<pre><code class=\"xml\">&lt;!DOCTYPE html> &lt;html> &lt;head>     &lt;title>Rust WebAssembly App&lt;\/title>     &lt;script type=\"module\">         import init, { say_hello } from '.\/my_web_project.js';          async function run() {             await init();             alert(say_hello());         }          run();     &lt;\/script> &lt;\/head> &lt;body>     &lt;h1>Hello from Rust and WebAssembly!&lt;\/h1> &lt;\/body> &lt;\/html><\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442 \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 WebAssembly-\u043c\u043e\u0434\u0443\u043b\u044c, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>say_hello<\/code>.<\/p>\n<hr\/>\n<p>Rust \u0438\u043c\u0435\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 WASM. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438<\/p>\n<p>\u0412 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0442\u0435\u043c\u044b \u0445\u043e\u0447\u0443 \u043d\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0435 \u0432\u0435\u0431\u0438\u043d\u0430\u0440\u044b \u043e\u0442 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432 \u0440\u044b\u043d\u043a\u0430 \u043f\u0440\u043e\u00a0<a href=\"https:\/\/otus.pw\/TUW2\/\">\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 unsafe Rust<\/a>\u00a0\u0438 \u043f\u0440\u043e \u0442\u043e,\u00a0<a href=\"https:\/\/otus.pw\/tR78\/\">\u043a\u0430\u043a Rust \u043f\u043e\u0431\u0443\u0436\u0434\u0430\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u044e<\/a>.  <\/p>\n<p>\u0410 \u0431\u043e\u043b\u044c\u0448\u0435 \u043a\u0443\u0440\u0441\u043e\u0432 \u043e\u0442 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u0432 OTUS \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 <a href=\"https:\/\/otus.pw\/OGvV\/\">\u043f\u043e\u043b\u043d\u043e\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435<\/a>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/788602\/\"> https:\/\/habr.com\/ru\/articles\/788602\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-366182","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/366182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=366182"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/366182\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=366182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=366182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=366182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}