Should I pass a mutable reference or transfer ownership of a variable in the context of FFI?
<p>I have a program that utilizes a Windows API via a C FFI (via winapi-rs). One of the functions expects a pointer to a pointer to a string as an output parameter. The function will store its result into this string. I'm using a variable of type <code>WideCString</code> for this string. </p>

<p>Can I "just" pass in a mutable ref to a ref to a string into this function (inside an unsafe block) or should I rather use a functionality like <code>.into_raw()</code> and <code>.from_raw()</code> that also moves the ownership of the variable to the C function? </p>

<p>Both versions compile and work but I'm wondering whether I'm buying any disadvantages with the direct way.</p>

<p>Here are the relevant lines from my code utilizing <code>.into_raw</code> and <code>.from_raw</code>.</p>

<pre><code>let mut widestr: WideCString = WideCString::from_str("test").unwrap(); //this is the string where the result should be stored
let mut security_descriptor_ptr: winnt::LPWSTR = widestr.into_raw();

let rtrn3 = unsafe {
    advapi32::ConvertSecurityDescriptorToStringSecurityDescriptorW(sd_buffer.as_mut_ptr() as *mut std::os::raw::c_void,
                                    1, 
                                    winnt::DACL_SECURITY_INFORMATION,
                                    &amp;mut security_descriptor_ptr,
                                        ptr::null_mut())

};

if rtrn3 == 0 {
    match IOError::last_os_error().raw_os_error() {
        Some(1008) =&gt; println!("Need to fix this errror in get_acl_of_file."), // Do nothing. No idea, why this error occurs
        Some(e) =&gt; panic!("Unknown OS error in get_acl_of_file {}", e),
        None =&gt; panic!("That should not happen in get_acl_of_file!"),
    }
}

let mut rtr: WideCString = unsafe{WideCString::from_raw(security_descriptor_ptr)};
</code></pre>

<p>The <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa376397(v=vs.85).aspx" rel="nofollow">description of this parameter in MSDN</a> says:</p>

<blockquote>
  <p>A pointer to a variable that receives a pointer to a <strong>null</strong>-terminated security descriptor string. For a description of the string format, see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa379570(v=vs.85).aspx" rel="nofollow">Security Descriptor String Format</a>. To free the returned buffer, call the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730(v=vs.85).aspx" rel="nofollow"><strong>LocalFree</strong></a> function.</p>
</blockquote>

<p>I am expecting the function to change the value of the variable. Doesn't that - per definition - mean that I'm moving ownership?</p>