How do you define an op in rust to return a string?
fails to compile
https://github.com/denoland/deno_core/blob/main/ops/op2/README.md#return-values this guide says I should mark string with
#[string] String
, however this also doesn't compile.
2 Replies
related: it'd be super nice if https://deno.com/blog/roll-your-own-javascript-runtime was updated to use
#[op2]
instead of the non-existent #[op]
😄 but that's not my immediate question
I found answer in https://github.com/denoland/roll-your-own-javascript-runtime/blob/main/src/main.rs#L26-L31
Frustrating that the doc is out of date, but at least underlying repo has the updated info. Mentioning on the blog that it's using a deprecated/stale version of deno_core, and the repo has updated examples would be super helpful.I wasn't able to find the answer to your question in the official Deno documentation. However, based on the error messages you've provided, it seems like there might be a misunderstanding in how to use the
#[string]
attribute with the return type in your Rust function.
In the Deno documentation, the #[string]
attribute is used to indicate that a parameter should be treated as a string when crossing the boundary between JavaScript and Rust. However, it's not used directly in the return type position.
Instead of using #[string]
in the return type, you should return a Result<String, AnyError>
from your op function. Here's an example of how you might define an op that returns a string:
Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of the advice given. It's also possible that there have been updates or changes to the Deno core library that are not yet reflected in the documentation, so you might want to look at the latest examples in the Deno core repository or ask for help from the Deno community.