rusted
Rust's syntax features for javascript.
These features will help our functional programming.
Feature
-
enum
- type checking
-
struct
- type checking
- mutable property
- tuple struct
- unit struct
-
impl
- static method (associated function)
-
trait
-
iter
- type alias
-
panic
(not a macro) -
match
-
Result
-
Option
Install
npm install --save rusted
Example
These examples require es6 transpiler like Babel.
enum
; //enum Message {// Quit,// ChangeColor(i32,i32,i32),// Move {x:i32, y:i32},// Write(String)//}let Message=; // with datalet x=Message; // let x: Message = Message::Move { x: 3, y: 4 };// without datalet y=MessageQuit; // let y: Message = Message::Quit;
struct
struct
and enum
checks type of property when instantiate.
'any'
- accepts any type
'number'
,'string'
,'object'
... (String)- compared to
typeof value_of_prop
- compared to
Object
,Array
,Number
... (Constructor)- compared to
value_of_prop.constructor
- compared to
; let Position=; let Player=; let player=; console; // > 0
impl
; // For structlet SomeStruct=; ; let some_struct=SomeStruct; // You don't need to attach "$" at first. "$" was removed at attaching proccesssome_struct; // > (2,3) // For enumlet SomeEnum=; ; let x=SomeEnumFooy=SomeEnum;console; // > Foo!console; // > Bar,7! // For ordinary constructor (javascript class)let { thisname='foo';}; ; let some_constructor=SomeConstructor;some_constructor; // > Hello, foo !
trait
; let Foo=; let BarTrait=; ;/* This would be errorimpl(BarTrait,Foo,{});*/ let foo=;foo; // > Tarofoo; // > hello
Option
; let { return denominator==0 ? None // represents there are no data : ;}; let result = ; ;// > Result: 0.666...
Result
; { ifx>5 return ; else return ; } console;// > Less than 5 ! console; // > 10console; // throws error
(es5 version.)
var rusted= Ok=rustedOk Err=rustedErr match=rustedmatch; { ifx>5 return ; else return ; } console; console;console;
License
MIT