[WASM + Rust] Debug a WebAssembly Module Written in Rust using console.log
2024-10-21 20:29:55
Having some kind of debugging tool in our belt is extremely useful before writing a lot of code. In this lesson we build a println!()-style syntax using JavaScript’s console.log to be able to log out values in Rust.
n JavaScript, the console log function is the way to log messages to the browser's developer tool console. Using Rust
wasm_bindgen
, we can import a reference to it.extern "C" {
...
#[wasm_bindgen(js_namespace = console)]
fn log(msg: &str);
}
Using the Rust macro, we can provide a inaudible style syntax for console log, logging from Rust.
#[wasm_bindgen]
pub fn run() {
let val = document.createElement("p");
log!("The {} is {}", "meaning of life", );
val.set_inner_html("Hello from Rust!!!!!!");
document.body().append_child(val);
}
Reload the server, then in the broswer console, we can see the console.log message from Rust.
最新文章
- 主流浏览器css兼容问题的总结
- c#生成MD5字符串
- Cyclic Nacklace[HDU3746]
- Java组待开发的任务
- windows8开发学习笔记
- java多线程并发编程与CPU时钟分配小议
- C#动态编程
- C++ 配置文件类的封装
- 使用idea启动springMVC+Hibernate其他项目
- Go语言之Interface(一)
- SQL跨服务器查询数据库
- FileReader读取文件详解
- Linux 中指定启动 tomcat 的 jdk 版本
- 【转】常用jar包的作用了解
- ActiveRecord 惰性加载,和使用gem faker
- gcd 模板
- 【BZOJ4317】Atm的树 动态树分治+二分+线段树
- CentOS 7防火墙设置开放80端口
- spring-data-redis RedisTemplate操作
- sanic官方文档解析之streaming(流动,滚动)和class_based_views(CBV的写法)