
該板條箱的目的是為Web API提供生鏽的綁定,並允許Rust和JavaScript之間具有高度的互操作性。
由於這些出色的人,該軟件被帶給您:
謝謝你!
您可以將JavaScript代碼直接嵌入Rust:
let message = "Hello, 世界!" ;
let result = js ! {
alert ( @ { message } ) ;
return 2 + 2 * 2 ;
} ;
println ! ( "2 + 2 * 2 = {:?}" , result ) ;還支持關閉:
let print_hello = | name : String | {
println ! ( "Hello, {}!" , name ) ;
} ;
js ! {
var print_hello = @ { print_hello } ;
print_hello ( "Bob" ) ;
print_hello . drop ( ) ; // Necessary to clean up the closure on Rust's side.
}您也可以通過Serde通過任意結構:
# [ derive ( Serialize ) ]
struct Person {
name : String ,
age : i32
}
js_serializable ! ( Person ) ;
js ! {
var person = @ { person } ;
console . log ( person . name + " is " + person . age + " years old." ) ;
} ;該板條箱還暴露了許多Web API,例如:
let button = document ( ) . query_selector ( "#hide-button" ) . unwrap ( ) . unwrap ( ) ;
button . add_event_listener ( move |_ : ClickEvent | {
for anchor in document ( ) . query_selector_all ( "#main a" ) {
js ! ( @ { anchor } . style = "display: none;" ; ) ;
}
} ) ;也支持將Rust功能暴露於JavaScript:
# [ js_export ]
fn hash ( string : String ) -> String {
let mut hasher = Sha1 :: new ( ) ;
hasher . update ( string . as_bytes ( ) ) ;
hasher . digest ( ) . to_string ( )
}然後,您可以從Node.js:
var hasher = require ( "hasher.js" ) ; // Where `hasher.js` is generated from Rust code.
console . log ( hasher . hash ( "Hello world!" ) ) ;或者,您可以使用相同的.js文件並在Web瀏覽器中使用它:
< script src =" hasher.js " > </ script >
< script >
Rust . hasher . then ( function ( hasher ) {
console . log ( hasher . hash ( "Hello world!" ) ) ;
} ) ;
</ script >如果您使用的是包裹,也可以使用我們的實驗包裹插件;首先在您現有的包裹項目中執行此操作:
$ npm install --save parcel-plugin-cargo-web
然後簡單:
import hasher from "./hasher/Cargo.toml" ;
console . log ( hasher . hash ( "Hello world!" ) ) ; 看看一些示例:
examples/minimal - 一個完全最小的示例,調用警報examples/todomvc一種天真的todomvc應用程序;顯示如何致電DOMexamples/hasher顯示如何將Rust功能導出到JavaScript以及如何從Vanilla Web瀏覽器環境或Nodejs撥打它們examples/hasher-parcel顯示如何在包裹項目中導入和調用出口的Rust功能pinky-web NES仿真器;您可以在此處使用預編譯版本API文檔也可以供您查看。
安裝貨物 - 韋布:
$ cargo install -f cargo-web
進入examples/todomvc ,並使用以下命令之一啟動示例:
使用Rust的本機WebAssembly後端編譯到WebAssmbly:
$ cargo web start --target=wasm32-unknown-unknown
使用Emscripten編譯為ASM.JS:
$ cargo web start --target=asmjs-unknown-emscripten
使用Emscripten編譯到WebAssembly:
$ cargo web start --target=wasm32-unknown-emscripten
請訪問http://localhost:8000 with瀏覽器。
對於*-emscripten而言,無需cargo-web ,但是不需要emscripten的本地wasm32-unknown-unknown不需要貨物,需要cargo-web工作!
stdweb 0.4.20
wasm-bindgen兼容性FullscreenChangeEvent stdweb 0.4.19
Document::fullscreen_enabledDocument::fullscreen_elementInputElement::selection_startInputElement::selection_endInputElement::set_selection_startInputElement::set_selection_endObject::to_iterWindow::confirm&Array現在可以通過TryFrom轉換為Vec sfutures相關的不穩定的API已更新以與最新的夜間合作syn依賴項已更新為版本1 stdweb 0.4.18
js!現在可以在use中導入宏BeforeUnloadEventUnloadEventIBlob::sliceIBlob::slice_with_content_typeIWindowOrWorker::set_clearable_timeout stdweb 0.4.17
futures相關的不穩定的API已更新以與最新的夜間合作stdweb 0.4.16
wasm-bindgen兼容性;您現在可以使用wasm-bindgen在項目中使用stdwebcargo-web版本現在為0.6.24 stdweb 0.4.15
futures相關的不穩定的API已更新以與最新的夜間合作FormDataFormDataEntryMouseButtonsStateBlob::new stdweb 0.4.14
js!如果您沒有從JS片段中返回任何內容,則宏現在會生成更有效的代碼。這使得在絕大多數情況下不必添加@(no_return)註釋。File stdweb 0.4.13
js!宏的空間處理ITouchEventTouchTouchTypeTouchEventTouchMoveTouchLeaveTouchEnterTouchEndTouchCancelTouchStartXmlHttpRequest::set_response_type stdweb 0.4.12
wasm32-unknown-unknown目標而沒有cargo-web改進了診斷方法stdweb 0.4.11
cargo-web版本現在為0.6.22wasm32-unknown-unknown現已正式支持穩定的Rustwasm32-unknown-unknown為基礎的調試js!現在使用程序宏部分實現宏Document::import_nodeIElement::slotIElement::attach_shadowIElement::shadow_rootISlotableShadowRootShadowRootModeTemplateElementSlotElementSlotChangeEventIParentNode::query_selector和IParentNode::query_selector_all現在返回正確的錯誤類型stdweb 0.4.10 , stdweb-derive 0.5.1
IElement::insert_adjacent_htmlIElement::insert_html_beforeIElement::insert_html_afterIElement::prepend_htmlIElement::append_htmlIElement::namespace_uriIElement::closestDocument::create_element_nsWindow::get_selectionAbortErrorSelectionTypeSelectionRange.try_into() / .try_from() )現在可轉換為TypeErrorDrawImageError ) js!宏TypeError (被錯誤地視為DOMException ).into() / .from()將Number轉換為f64Mut ,這是一種新的包裝類型,可安全地將FnMut封閉插入js!宏;目前是可選的,但是這種包裝紙的使用將來將是強制性的!FnMut關閉不能再遞歸地稱為#[derive(ReferenceType)]現在支持一個有限的通用類型子集#[async_test]屬性宏支持異步單元測試(僅每晚)futures 0.3 (僅每晚) stdweb 0.4.9 , stdweb-derive 0.5.0
ScrollEventDragRelatedEventDragEventDragStartEventDragEndEventDragEnterEventDragLeaveEventDragOverEventDragExitEventDragDropEventDataTransferEffectAllowedDropEffectDataTransferItemListDataTransferItemDataTransferItemKindIDragEventValue S現在可以轉換為try_into Option< Serde< T > >i32和f64以外的其他類型keyup事件中進行KeyDownEvent化,因為僅檢查了事件的JS類型,並且keyup和keydown共享相同的JS類型( KeyboardEvent )。從現在開始,還檢查了事件的type字段,因此不再允許這樣的轉換。 0.4.8
wasm32-unknown-unknown最新夜間彙編PointerLockChangeEventPointerLockErrorEventMouseWheelEventMouseWheelDeltaModeXhrResponseTypeXmlHttpRequest::raw_responseWindow::device_pixel_ratioDocument::pointer_lock_elementDocument::exit_pointer_lock 0.4.7
AuxClickEventMouseEnterEventMouseLeaveEventContextMenuEventSelectionChangeEventFileListFileReaderReadyStateGamepadGamepadButtonGamepadButtonMappingGamepadEventCanvasRenderingContext2d::clear_rectVec S和ArrayBuffer s創建TypedArray時的洩漏。 0.4.6
docs.rsSubmitEventIChildNodeCanvasElement::to_data_url 0.4.5
DocumentFragmentSelectElementOptionElementHtmlCollectionNode::from_htmlValue::is_nullSocketMessageDataNodeTypefutures 0.2 0.4.4
docs.rs (希望)。Location::originLocation::protocolLocation::hostLocation::hostnameLocation::portLocation::pathnameLocation::searchSecurityError :Location::hashLocation::href 0.4.3
WeakMap中鍵的對象(例如,在Firefox下的一些與WebGL相關的對象)Element::get_bounding_client_rectElement::scroll_topElement::scroll_leftWindow::page_x_offsetWindow::page_y_offsetNodeList::itemDocument::bodyDocument::headDocument::titleDocument::set_titleIMouseEvent::offset_xIMouseEvent::offset_yCompositeOperationLineCapLineJoinRepetitionTextAlignTextBaselineAddColorStopError , DrawImageError , GetImageDataErrorMouseOverEventMouseOutEventPointerOverEventPointerEnterEventPointerDownEventPointerMoveEventPointerUpEventPointerCancelEventPointerOutEventPointerLeaveEventGotPointerCaptureEventLostPointerCaptureEventIPointerEvent 0.4.2
CanvasRenderingContext2d::get_canvasFillRule和SocketReadyStateIElement新屬性與屬性相關的方法Date綁定0.4.1
wasm32-unknown-unknown最新夜間生鏽SocketBinaryType枚舉CanvasRenderingContext2d的許多新方法CanvasGradient , CanvasPattern , CanvasStyle , ImageData , TextMetricsIndexSizeError , NotSupportedError , TypeError 0.4
Value中刪除了Array和Object變體;這些現在被視為ReferenceValue具有額外的變體: SymbolInputElement::set_kindInputElement::filesKeydownEvent > KeyDownEventKeyupEvent > KeyUpEventKeypressEvent > KeyPressEventReadyState > FileReaderReadyStateInputElement::value > InputElement::raw_valueInputElement::set_value > InputElement::set_raw_valueArrayBuffer::new現在採用u64參數InputElement::set_raw_value現在使用&str而Into< Value >usize的每種方法現在返回u32INode::remove_child現在在Ok情況下返回Nodeu64 :ArrayBuffer::leni32而不是f64 :IMouseEvent::client_xIMouseEvent::client_yIMouseEvent::movement_xIMouseEvent::movement_yIMouseEvent::screen_xIMouseEvent::screen_yResult :INode::insert_beforeINode::replace_childINode::clone_nodeStringMap::insertTokenList::addTokenList::removeDocument::create_elementIEventTarget::dispatch_eventFileReader::read_as_textFileReader::read_as_array_bufferFileReader::read_as_textHistory::replace_stateHistory::goHistory::backHistory::forwardLocation::hrefLocation::hashCanvasElement::to_data_urlCanvasElement::to_blobArrayBuffer::newINode::base_uri現在返回String ,而不是Option< String >InputElement::raw_value現在返回String而不是ValueINode::inner_text移動到IHtmlElement::inner_textDocument::query_selector和Document::query_selector_all移動到IParentNodeIElement::query_selector IParentNode IElement::query_selector_all )Document::get_element_by_id已移至INonElementParentNodeTryFrom / TryInto在任意參考的對象之間轉換的毯子式cargo-web構建時,不必再調用stdweb::initialize stdweb::event_loopwasm32-unknown-unknown cdylib板條板上的支持XmlHttpRequestWebSocketMutationObserverHistoryTextAreaElementCanvasElementMouseDownEventMouseUpEventMouseMoveEventPopStateEventResizeEventReadyStateChangeSocketCloseEventSocketErrorEventSocketOpenEventSocketMessageEventReferenceType和InstanceOfstdweb-derive板條箱中添加#[derive(ReferenceType)] ;現在可以在stdweb之外定義自定義API綁定#[js_export]程序屬性(僅wasm32-unknown-unknown )DomException和子類型,以使JavaScript異常傳遞IElement現在從INode繼承ReferenceType繼承stdweb::traits模塊充當use前奏 - 我們所有的界面特徵console!宏PartialEq和Eq 0.3
ErrorEvent方法LoadEvent > ResourceLoadEventAbortEvent > ResourceAbortEventErrorEvent > ResourceErrorEventUnsafeTypedArray js!Once以將FnOnce關閉到js! 根據任何一個
可以選擇。
來自Mozilla開發人員網絡的文檔片段涵蓋了CC-BY-SA版本2.5或更高版本。
參見貢獻