title: How do I return an vector of dynamic length in a pub extern "C" fn?
body: <p>I want to return a vector in a <code>pub extern "C" fn</code>. Since a vector has an arbitrary length, I guess I need to return a struct with </p>
  
  <ol>
  <li><p>the pointer to the vector, and</p></li>
  <li><p>the number of elements in the vector</p></li>
  </ol>
  
  <p>My current code is:</p>
  
  <pre><code>extern crate libc;
  use self::libc::{size_t, int32_t, int64_t};
  
  // struct to represent an array and its size
  #[repr(C)]
  pub struct array_and_size {
      values: int64_t, // this is probably not how you denote a pointer, right?
      size: int32_t,
  }
  
  // The vector I want to return the address of is already in a Boxed struct, 
  // which I have a pointer to, so I guess the vector is on the heap already. 
  // Dunno if this changes/simplifies anything?
  #[no_mangle]
  pub extern "C" fn rle_show_values(ptr: *mut Rle) -&gt; array_and_size {
      let rle = unsafe {
          assert!(!ptr.is_null());
          &amp;mut *ptr
      };
  
      // this is the Vec&lt;i32&gt; I want to return 
      // the address and length of
      let values = rle.values; 
      let length = values.len();
  
      array_and_size {
         values: Box::into_raw(Box::new(values)),
         size: length as i32,
         }
  }
  
  #[derive(Debug, PartialEq)]
  pub struct Rle {
      pub values: Vec&lt;i32&gt;,
  }
  </code></pre>
  
  <p>The error I get is </p>
  
  <pre><code>$ cargo test
     Compiling ranges v0.1.0 (file:///Users/users/havpryd/code/rust-ranges)
  error[E0308]: mismatched types
    --&gt; src/rle.rs:52:17
     |
  52 |         values: Box::into_raw(Box::new(values)),
     |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected i64, found *-ptr
     |
     = note: expected type `i64`
     = note:    found type `*mut std::vec::Vec&lt;i32&gt;`
  
  error: aborting due to previous error
  
  error: Could not compile `ranges`.
  
  To learn more, run the command again with --verbose.
  -&gt; exit code: 101
  </code></pre>
  
  <p>I posted the whole thing because I could not find an example of returning arrays/vectors in the eminently useful <a href="http://jakegoulding.com/rust-ffi-omnibus/" rel="nofollow">Rust FFI Omnibus</a>.</p>
  
  <p>Is this the best way to return a vector of unknown size from Rust? How do I fix my remaining compile error? Thanks!</p>
  
  <p>Bonus q: if the fact that my vector is in a struct changes the answer, perhaps you could also show how to do this if the vector was not in a Boxed struct already (which I think means the vector it owns is on the heap too)? I guess many people looking up this q will not have their vectors boxed already. </p>
  
  <p>Bonus q2: I only return the vector to view its values (in Python), but I do not want to let the calling code change the vector. But I guess there is no way to make the memory read-only and ensure the calling code does not fudge with the vector? <code>const</code> is just for showing intent, right?</p>
  
  <p>Ps: I do not know C or Rust well, so my attempt might be completely WTF. </p>