NERDSummit Slack: #room145
A language empowering everyone to build reliable and efficient software.- https://rust-lang.org/
let checked: boolean = true;
let height: u32 = 8;
let name: &str = "Testing";
enum PointType {
Dot,
Circle,
Square
}
struct Point {
x: u32,
y: u32,
type: PointType
}
const ORIGIN: Point = Point { x: 0, y: 0, type: PointType::Dot };
enum Shape {
Circle(radius: u32),
Square(size: u32),
Rectangle(width: u32, height: u32)
}
const FOUR_SQUARE: Shape = Shape::Square(4);
enum Option<T> {
None,
Some(T)
}
const ZERO_SOME: Option<u32> = Option::Some(0);
pub fn double(x: u32) -> u32 {
x * 2
}
pub fn fib(x: u32) -> u32 {
match x {
0 | 1 => x,
_ => x + fib(x - 1)
}
}
pub fn gt_or_lt_label(x: u32, y: u32) -> String {
let is_gt = x > y; // is_gt: boolean
let ret = // ret: String
if is_gt {
"Greater Than".into_string()
} else {
"Less Than".into_string()
};
ret
}
impl Option<T> {
pub fn is_some(&self: Self) -> boolean {
match self {
Some(_) => true,
_ => false,
}
}
}
assert_eq!(
Some("Is that you, John Wayne?").is_some(),
true
);
pub fn point_type(p: Point) -> PointType {
let Point { _x, _y, type } = p;
type
}
pub fn increase(s: Shape) -> Shape {
match s {
Shape::Circle(r) => Shape::Circle(r * 2),
Shape::Square(s) => Shape::Square(s * 2),
Shape::Rectangle(w, h) => Shape::Rectangle(w * 2, h * 2),
}
}
let p1: Point = Point { x: 1, y: 2, type: PointType::Dot };
p1.x = 2; // rustc won't allow this!
let mut p2: Point = Point { x: 1, y: 2, type: PointType::Dot };
p2.x = 2;
let s: &str = "Testing";
// Really translates to special
let s: &'static str = "Testing"; // Static string!
pub fn len(s: &str) -> usize {
// Some implementation
}
// Non-elided
pub fn len<'a>(s: &'a str) -> usize {
// Some implementation
}
let p1: Point = Point { x: 1, y: 2, type: PointType::Dot };
p1.x = 2; // rustc won't allow this!
let mut p2: Point = Point { x: 1, y: 2, type: PointType::Dot };
p2.x = 2;
(module
(func $add (param $lhs i32) (param $rhs i32) (result i32)
local.get $lhs
local.get $rhs
i32.add)
(export "add" (func $add))
)
$ rustup show
Default host: x86_64-unknown-linux-gnu
rustup home: /home/peter/.rustup
installed toolchains
--------------------
stable-x86_64-unknown-linux-gnu
nightly-x86_64-unknown-linux-gnu
installed targets for active toolchain
--------------------------------------
wasm32-unknown-unknown
x86_64-unknown-linux-gnu
active toolchain
----------------
stable-x86_64-unknown-linux-gnu (default)
rustc 1.39.0 (4560ea788 2019-11-04)
#[no_mangle]
pub fn fib(x: u32) -> u32 {
match x {
0 | 1 => x,
_ => x + fib(x - 1)
}
}
rustc --target wasm32-unknown-unknown --crate-type cdylib fib.rs
fetch("fib.wasm")
.then(t => t.arrayBuffer())
.then(b => WebAssembly.instantiate(b))
.then(w => w.instance.exports)
.then(fibber => fibber.fib(5));
Import JavaScript things into Rust and export Rust things to JavaScript.- https://github.com/rustwasm/wasm-bindgen
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
[dependencies]
wasm-bindgen = "0.2.59"
[dependencies.web-sys]
version = "0.3.4"
features = [
'Document',
'Element',
'HtmlElement',
'Node',
'Window',
]
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
let window = web_sys::window().expect("no `window`!");
let document = window.document().expect("no `document`!");
let body = document.body().expect("no `body`!");
let val = document.create_element("p")?;
val.set_inner_html("Hello from Rust!");
body.append_child(&val)?;
Ok(())
}
$ npm init rust-webpack my-rust-intro
npx: installed 18 in 4.972s
🦀 Rust + 🕸 WebAssembly + Webpack = ❤️
Installed dependencies ✅
$ ls -1 my-rust-intro/
Cargo.toml
js
node_modules
package.json
package-lock.json
README.md
src
static
tests
webpack.config.js
use yew::{html, Callback, ClickEvent, Component, ComponentLink, Html, ShouldRender};
struct App {
clicked: bool,
onclick: Callback<ClickEvent>,
}
enum Msg {
Click,
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
App {
clicked: false,
onclick: link.callback(|_| Msg::Click),
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Click => {
self.clicked = true;
true // Indicate that the Component should re-render
}
}
}
fn view(&self) -> Html {
let button_text = if self.clicked { "Clicked!" } else { "Click me!" };
html! {
<button onclick=&self.onclick>{ button_text }</button>
}
}
fn main() {
yew::start_app::<App>();
}
https://petejohanson.gitlab.io/hyperworm/
https://gitlab.com/petejohanson/hyperworm/