title: Rust interop with C++ std::string
body: <p>I'm trying to build Octave functions in Rust. Octave's API is in C++, so I've generated bindings using rust-bindgen. I'm currently working through the problems that occur when trying to generate <a href="https://github.com/ctaggart/octh/issues/28" rel="noreferrer">bindings that include <code>std::string</code></a>. It would be nice if I could leave it opaque and valid pointer to a <a href="https://gcc.gnu.org/onlinedocs/libstdc++/manual/strings.html" rel="noreferrer">C++ <code>std::string</code></a>. Would it be possible to build a utility function on the C++ side any time I needed to pass in a C++ <code>std::string</code>?</p>
  
  <p>I was naive when I first attempted this. It is clearly wrong. A Rust <a href="https://doc.rust-lang.org/std/ffi/struct.CString.html" rel="noreferrer"><code>std::ffi:CString</code></a> is for C strings, not C++ strings. I found <a href="https://embeddedartistry.com/blog/2017/7/24/stdstring-vs-c-strings" rel="noreferrer">this recent blog</a> helpful when comparing the two. My first attempt looks <a href="https://github.com/ctaggart/octh_examples/blob/723cfaf9fc6d16e0c0caec6148ecf1e1dbc62831/src/lib.rs" rel="noreferrer">like this</a>:</p>
  
  <pre class="lang-rust prettyprint-override"><code>#![allow(non_snake_case)]
  #![allow(unused_variables)]
  
  extern crate octh;
  
  // https://thefullsnack.com/en/string-ffi-rust.html
  use std::ffi::CString;
  
  #[no_mangle]
  pub unsafe extern "C"  fn Ghelloworld (shl: *const octh::root::octave::dynamic_library, relative: bool) -&gt; *mut octh::root::octave_dld_function {
      let name = CString::new("helloworld").unwrap();
      let pname = name.as_ptr() as *const octh::root::std::string;
      std::mem::forget(pname);
  
      let doc = CString::new("Hello World Help String").unwrap();
      let pdoc = doc.as_ptr() as *const octh::root::std::string;
      std::mem::forget(pdoc);
  
      octh::root::octave_dld_function_create(Some(Fhelloworld), shl, pname, pdoc)
  }    
  
  pub unsafe extern "C" fn Fhelloworld (args: *const octh::root::octave_value_list, nargout: ::std::os::raw::c_int) -&gt; octh::root::octave_value_list {
      let list_ptr = ::std::ptr::null_mut();
      octh::root::octave_value_list_new(list_ptr);
      ::std::ptr::read(list_ptr)
  }
  </code></pre>
  
  <p>I need to pass in the function name and documentation as strings to <code>octave_dld_function_create</code>. I wish there was a <code>CppString</code> that I could use instead. Any suggestions on how to proceed? </p>