Table of content

What is this :grey_question:

The rules of the game are explained in my original post. I haven’t posted in a while but I want to get started again.

49th Challenge

Challenge

Today the challenge will be simple. Use rust bindings to create a hello world. Also, because there is a greate hype around AI generating code and images from text I will use both in this post.

Solution :white_check_mark:

Ferris the crab from the rust programming language drawn as marvel comic character. Created using stable diffusion

Ferris the crab from the rust programming language drawn as gta character.

We will use code pilot from github to generate the javascript binding. With the promt /* bind javascript */ the first prompt is as follows

    fn main() {
    /* bind javascript */
    let js = include_str!("../js/main.js");
	

This looks good at first glance but will actually jsut print out the text contained in our main.js. main.js was also generated with a prompt contains the text

    /*Hello world javascript*/
    console.log("Hello world");
    

Sadly that was the only suggestion, so I try with a different approach. Lets try again..

    /* use js macro cargo */
    use js::js;

    fn main() {
    /* bind javascript helloWorld() from js/main.js to rust as function */
    let hello_world = js! {
        function helloWorld() {
            console.log("Hello World!");
        }
    };

    println!("{}",hello_world);

    }
    

Again it looks promising, but it does not work either, so I give up and do some googling. Rust by Example tells us to use the cargo nodejs-helper so lets try to add this to the prompt. Basically the helper just runs the javascript in a nodejs, the repos is found here.

    use nodejs_helper::*;
    use wasm_bindgen::prelude::*;

    #[wasm_bindgen]
    pub fn hello_world() {
    nodejs_helper::console::log("Timestamp now: ");
    }

    fn main() {
    hello_world()
    }
    

However this is not what I’m trying to achive, this won’t run like a rust application. So I looked further and found wasmedge-quickjs. With this solution you also require wasmedge installed to run. So the code

    use wasmedge_quickjs::*;

    fn main() {
        let mut ctx = Context::new();
        let code = r#"print('hello quickjs')"#;
        let r = ctx.eval_global_str(code);
        println!("return value:{:?}", r);
    }
    

is compiled using $ cargo build --target wasm32-wasi --release and can then be run using wasmedge --dir .:. target/wasm32-wasi/release/wasmedge_quickjs.wasm

This is a somewhat working solution but I will probabyl explore this some more in the future. For now the challenge for today is done.

See github for today’s simple snippet.