開始
當附加到函式時,此屬性會設定要發出的 Wasm 可執行檔的 start
區段,在 Wasm 模組實例化後立即執行標記的函式。
# #![allow(unused_variables)] #fn main() { #[wasm_bindgen(start)] fn start() { // executed automatically ... } #}
Wasm 可執行檔的 start
區段將被設定為在此處盡快執行 start
函式。請注意,由於目前各種實際限制,可執行檔的 start 區段可能不會直接指向 start
,但此處的 start
函式應在載入 wasm 模組時自動啟動。
使用 start
屬性時,有一些注意事項需要注意
start
函式不得接受任何引數,且必須回傳()
或Result<(), JsValue>
- 一個模組中只能放置一個
start
函式,包括其相依性。如果指定多個,則當執行 CLI 時,wasm-bindgen
將會失敗。建議只有應用程式才使用此屬性。 - 測試時不會執行
start
函式。 - 請注意,
start
函式是相對較新的功能,因此如果您發現任何錯誤,請隨時回報問題!