本文簡要介紹rust語言中 std::ffi::CString.new
的用法。
用法
pub fn new<T: Into<Vec<u8>>>(t: T) -> Result<CString, NulError>
從字節容器創建一個新的 C-compatible 字符串。
此函數將使用提供的數據並使用底層字節構造一個新字符串,確保有一個尾隨 0 字節。此函數將附加此尾隨 0 字節;提供的數據中不應包含任何 0 字節。
例子
use std::ffi::CString;
use std::os::raw::c_char;
extern "C" { fn puts(s: *const c_char); }
let to_print = CString::new("Hello!").expect("CString::new failed");
unsafe {
puts(to_print.as_ptr());
}
錯誤
如果提供的字節包含內部 0 字節,此函數將返回錯誤。返回的 NulError
將包含字節以及 nul 字節的位置。
相關用法
- Rust CString.into_bytes用法及代碼示例
- Rust CString.from_raw用法及代碼示例
- Rust CString.into_boxed_c_str用法及代碼示例
- Rust CString.into_raw用法及代碼示例
- Rust CString.as_c_str用法及代碼示例
- Rust CString.from_vec_unchecked用法及代碼示例
- Rust CString.from_vec_with_nul_unchecked用法及代碼示例
- Rust CString.from_vec_with_nul用法及代碼示例
- Rust CString.as_bytes用法及代碼示例
- Rust CString.into_bytes_with_nul用法及代碼示例
- Rust CString.as_bytes_with_nul用法及代碼示例
- Rust CString.into_string用法及代碼示例
- Rust CString用法及代碼示例
- Rust CStr.into_c_string用法及代碼示例
- Rust CStr.from_bytes_with_nul用法及代碼示例
- Rust CStr.from_bytes_with_nul_unchecked用法及代碼示例
- Rust CStr用法及代碼示例
- Rust CStr.from_ptr用法及代碼示例
- Rust CStr.to_bytes用法及代碼示例
- Rust CStr.as_ptr用法及代碼示例
- Rust CStr.to_str用法及代碼示例
- Rust CStr.to_string_lossy用法及代碼示例
- Rust CStr.to_bytes_with_nul用法及代碼示例
- Rust Cursor.new用法及代碼示例
- Rust Condvar.notify_all用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::ffi::CString.new。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。