xref: /wasmtime-44.0.1/examples/gcd.rs (revision 9ce3ffe1)
1 //! Example of instantiating of the WebAssembly module and invoking its exported
2 //! function.
3 
4 // You can execute this example with `cargo run --example gcd`
5 
6 use wasmtime::*;
7 
main() -> Result<()>8 fn main() -> Result<()> {
9     // Load our WebAssembly (parsed WAT in our case), and then load it into a
10     // `Module` which is attached to a `Store` cache. After we've got that we
11     // can instantiate it.
12     let mut store = Store::<()>::default();
13     let module = Module::from_file(store.engine(), "examples/gcd.wat")?;
14     let instance = Instance::new(&mut store, &module, &[])?;
15 
16     // Invoke `gcd` export
17     let gcd = instance.get_typed_func::<(i32, i32), i32>(&mut store, "gcd")?;
18 
19     println!("gcd(6, 27) = {}", gcd.call(&mut store, (6, 27))?);
20     Ok(())
21 }
22