1

I want to calculate the Fibonacci sequence result. Here is my Rust code which will return the series of a given number.

#[no_mangle]
pub extern "C" fn fibonacci(n: usize) -> Vec<i32> {
    let mut x = vec![1, 1];
    for i in 2..n {
        let next_x = x[i - 1] + x[i - 2];
        x.push(next_x)
    }
    x
}

The result from cargo run [1, 1, 2] When I call this code in go-ext-wasmer, I'm getting the invalid output.

    bytes, _ := wasm.ReadBytes("fib.wasm")
    instance, _ := wasm.NewInstance(bytes)
    defer instance.Close()
    randoms, err := instance.Exports["fibonacci"](3, 0)

    resp := randoms.ToI32()
    if err != nil {
        fmt.Println("error", err)
    }
    memory = instance.Memory.Data()
    fmt.Println("fibonacci:", memory[resp:resp+10])

Result from go fibonacci: [10 0 0 144 0 17 0 2 0 0 0 2 0 0 0]

How do I get the correct return from go? I wanted a series which should be in []byte form

achala
  • 197
  • 1
  • 11
  • Use an array instead of a vector. I'm not familiar with Go and Rust but it looks like you're trying to read the byes of a vector directly, which may be impossible if it's not POD (Plain Old Data), which a vector is not. At least in C++. – GirkovArpa Aug 13 '20 at 19:36

0 Answers0