I have a Hello World WebAssembly and I tried to add some code to show the time.
The following line appears to kill the function and it returns nothing (no text, no error)
let dt = Utc::now();
If I comment out the line the function runs as before and returns a string.
Is this happening to anyone else?
I have the 2 lines below at the top of my rs file:
extern crate chrono;
use chrono::{Duration, Utc};
I have the following in the dependencies in the toml file:
chrono = "0.4"
To be used in WASM,
chronomust be compiled withwasmbindfeature.I wasn't able to find this in documentation, however. This feature was referenced in source code:
Also, there's an open issue for documenting this behavior.
There are several reasons for the behavior you see.
chronocalls theget_timefunction from thetimecrate, which then can delegate to thelibcor something, depending on the target. However, when you compile to WASM, standard library is rather limited - there are no system calls, for example, and so a bunch of functionality must be provided in some other ways; in particular, aforementionedget_timefunction is explicitly unimplemented for this target, so that any call to it will panic.js-sys) is necessary only for some targets, it will be hidden behind feature flag, so that users of library (here,chrono) on e.g. Windows or Linux will not pull it unnecessarily. That's why you need to enable the feature explicitly, even if without it the library will be unusable on your target.console_error_panic_hookcrate, which, if added to your project, would show you the "not yet implemented" error in the console window.