title: How to update libc::c_char array with String?
body: <p>I have write some FFI code to C/C++,</p>
  
  <pre><code>use libc::c_char;
  use std::ffi::CString;
  
  type arr_type = [c_char; 20]; // arr_type is the type in C
  let mut arr : arr_type = [0; 20]; 
  
  let s = "happy123";
  let c_s = CString::new(s).unwrap();
  let s_ptr = c_s.as_ptr();
  </code></pre>
  
  <p>How can I update <code>arr</code> with the String <code>s</code>? In C/C++ I can use <code>memcpy</code>, <code>strcpy</code> etc...</p>
  
  <p>I have tried many, like use rlibc::memcpy and found it can't be used with libc.. , but the compiler don't let me pass, there is very few info about array in Rust.</p>
  
  <hr>
  
  <p>Add:
  After read the replys, I want to add some info and more question.</p>
  
  <p>1.</p>
  
  <p>In C++, I use <code>strcpy_s</code> to copy string to char array, cause the length of string and the size of array are all known.</p>
  
  <p>I have tried both methods below.</p>
  
  <p>the <code>std::iter::Zip</code> method, seems very like <code>strcpy_s</code>, but I don't know if there is some performance affect.</p>
  
  <p>the <code>copy_nonoverlapping</code> method, it use <code>as_mut_ptr()</code> cast the array to pointer then there is no length info, since it is in <code>unsafe { }</code> block, and I have tried copy a string longer then the array and there is no error displays... I wonder if that's ok?</p>
  
  <p>And is there a function in Rust like strcpy_s in C++?</p>
  
  <p>2.</p>
  
  <p>I'm using windows and msvc, for the char array, I mean <code>not deal with encoding</code> or use <code>default codepage encoding</code>.</p>
  
  <p>below are all ok in source file:</p>
  
  <p>C++:</p>
  
  <pre><code>std::string s = "world is 世界";
  std::wstring ws = L"world is 世界";
  </code></pre>
  
  <p>Qt:</p>
  
  <pre><code>QString qs = QStringLiteral("world is 世界");
  </code></pre>
  
  <p>Python 3:</p>
  
  <pre><code>s = 'world is 世界'
  </code></pre>
  
  <p>But in Rust, below may be wrong? as I see in Eclipse Debug window.</p>
  
  <pre><code>let s = "world is 世界";
  </code></pre>
  
  <p>I found rust-encoding and tried these:</p>
  
  <pre><code>use encoding::{Encoding, EncoderTrap};
  use encoding::all::GB18030;
  
  let s = "world is 世界";  
  let enc = GB18030.encode(&amp;s , EncoderTrap::Strict);
  </code></pre>
  
  <p>Is there any better way to do in Rust?</p>