当前位置: 首页 > java >正文

Rust: CString、CStr和String、str

在FFI与C交互中,少不了与C中字符串交互。在Rust中,有

各种String存在的意义:
OsString:因为要与操作系统等复杂的世界交互;
因为Rust世界中的Strings 始终是有效的 UTF-8。对于非 UTF-8 字符串,可以用到OsString。
CString: 与C的世界进行交互;
String:在Rust的世界中交互;

一、CString、String等代码探析

use std::ffi::{CStr, CString,c_char};
use std::borrow::Cow;
fn main() {println!("Hello, world!");show_cstring_bytes(CString::new("Hello, world!").expect("CString::new failed"));show_string_bytes("Hello, world!".to_string());}// as:不consume
// from/into:consume ownship
// into_bytes(),as_bytes()返回的缓冲区不包含尾随 nul 终止符,并且保证不包含任何内部 nul 字节。
// 必须用as_bytes_with_nul()返回的缓冲区包含 nul 终止符。
fn show_cstring_bytes_no_null(s:CString){let c_string_bytes = s.as_bytes();println!("c_string_bytes no null   : {:?}  len: {:?}", c_string_bytes,c_string_bytes.len());
}
fn show_cstring_bytes(s:CString){let c_string_bytes = s.as_bytes_with_nul();println!("c_string_bytes with null : {:?}  len: {:?}", c_string_bytes,c_string_bytes.len());}
fn show_string_bytes(s:String){let string_bytes = s.into_bytes();println!("  string_bytes           : {:?} len :{:?}", string_bytes,string_bytes.len());
}
// CString ->&CStr
fn cstring_to_cstr(s:&CString) ->&CStr{s.as_c_str()}
fn show_cstr_bytes_no_null(s:&CStr){let c_str_bytes = s.to_bytes();println!("c_str_bytes   no null: {:?}  len: {:?}", c_str_bytes,c_str_bytes.len());
}
fn show_cstr_bytes_with_null(s:&CStr){let c_str_bytes = s.to_bytes_with_nul();println!("c_str_bytes with null: {:?}  len: {:?}", c_str_bytes,c_str_bytes.len());
}
fn cstring_to_str(s:&CString) ->&str{s.to_str().expect("CString to str failed")
}
// *const c_char具体是*const i8 还是 *const u8由平台决定。
fn get_ptr_c_char() ->*const c_char{const BYTES: &[u8] = b"Hello, world! to c_char!\0";//或是:BYTES.as_ptr().cast()BYTES.as_ptr() as *const _ 
}fn get_cstr_from_bytes<'a>() ->&'a CStr{const BYTES_: &[u8] = b"Hello, world! from bytes!\0";let cstr = CStr::from_bytes_with_nul(BYTES_).expect("CStr::from_bytes_with_nul failed");cstr
}
fn get_cstr_from_ptr_c_char<'a>(s:*const c_char) ->&'a CStr{unsafe { CStr::from_ptr(s) }
}fn get_cstring() ->CString{let c_string = CString::new("Hello, world! from c string!").expect("CString::new failed");c_string}
fn check_cstring(s: *const c_char) -> bool{unsafe {let slice = CStr::from_ptr(s);let my_str = slice.to_str();match my_str{Ok(_) => return true,Err(_) => return false,};//println!("my_str: {}", my_str.expect("CStr::from_ptr failed"));}}fn cstr_to_str(s:&CStr) ->&str{s.to_str().expect("CStr::from_ptr failed")
}
fn cstring_to_cow_str(s:&CString) ->Cow<'_,str>{//let c_string = CString::new("Hello, world! from c string!").expect("CString::new failed");let c_string_ptr = s.as_ptr();let cow = unsafe { CStr::from_ptr(c_string_ptr).to_string_lossy() }; // COW<'_,str>cow
}
fn cstr_to_cow_str(s:&CStr) ->Cow<'_,str>{s.to_string_lossy()
}fn cstring_to_box_cstr(s:CString) ->Box<CStr>{s.into_boxed_c_str()
}
fn box_cstr_to_cstring(s:Box<CStr>) ->CString{s.into_c_string()
}fn vec_u8_with_null_to_cstring_unchecked(v:Vec<u8>) ->CString{unsafe{CString::from_vec_with_nul_unchecked(v)}
}
fn vec_u8_with_null_to_cstring_checked(v:Vec<u8>) ->CString{CString::from_vec_with_nul(v).expect("CString::from_vec_with_nul failed")
}fn vec_u8_no_null_to_cstring(v:Vec<u8>) ->CString{unsafe{CString::from_vec_unchecked(v)}
}
fn bytes_with_null_to_cstr_unchecked(bytes:&[u8]) ->&CStr{unsafe{ CStr::from_bytes_with_nul_unchecked(bytes) }
}
fn bytes_with_null_to_cstr_check(bytes:&[u8]) ->&CStr{unsafe{ CStr::from_bytes_with_nul(bytes).unwrap() }
}
fn bytes_no_null_to_cstr(bytes:&[u8]) ->&CStr{unsafe{ CStr::from_bytes_until_nul(bytes).unwrap() }
}
// MUST *mut : move ownership
fn ptr_to_cstring(ptr:*mut c_char) ->CString{unsafe{ CString::from_raw(ptr) }
}
// MUST:*mut : consume ownership
fn cstring_to_ptr_with_consume(s:CString) ->*mut c_char{s.into_raw() // s 被消耗,不能再使用
}
fn cstring_to_ptr_no_consume(s:&CString) ->*const c_char{s.as_ptr()
}
fn ptr_to_cstr<'a>(ptr: *const i8) ->&'a CStr{unsafe{ CStr::from_ptr(ptr) }
}
fn cstring_to_string(s:CString) ->String{// let c_string_ptr = s.as_ptr();// let my_string = unsafe { CStr::from_ptr(c_string_ptr).to_string_lossy() }; // COW<'_,str>// println!("my_string: {}", my_string);s.into_string().unwrap() // 消耗s,不能再使用}
fn string_to_cstring(s: String) ->CString{let c_string = CString::new(&*s).expect("CString::new failed");c_string
}

二、输出结果

Hello, world!
c_string_bytes with null : [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33, 0]  len: 14string_bytes           : [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33] len :13  

可以看到,在CString和String转化为字节后的本质区别。

相关的转化具体见上面的代码,有助于加深认识。

http://www.xdnf.cn/news/9500.html

相关文章:

  • 电商售后服务系统与其他系统集成:实现售后流程自动化
  • Eclipse 插件开发 5.3 编辑器 监听输入
  • AI Agent工具全景解析:从Coze到RAGflow,探索智能体自动化未来!
  • Java、Python、PHP 三种语言实现 二进制与十六进制的相互转换
  • 板凳-------Mysql cookbook学习 (八)
  • Java开发经验——阿里巴巴编码规范实践解析4
  • HTML5 视频播放器:从基础到进阶的实现指南
  • TypeScript 索引签名:灵活处理动态属性对象
  • STM32通过KEIL pack包轻松移植LVGL,并学会使用GUI guider
  • CRM系统的数据库结构详细设计
  • 【大模型原理与技术-毛玉仁】第四章 参数高效微调
  • 基本面高股息策略
  • RabbitMQ 与其他 MQ 的对比分析:Kafka/RocketMQ 选型指南(二)
  • c++结构化绑定
  • Python应用while循环猜数字
  • webpack的安装
  • 签约!京东云与契约锁达成战略合作,携手共推全程数智化解决方案
  • 【计算机网络】IPv6和NAT网络地址转换
  • 【Prometheus+Grafana实战:搭建监控系统(含告警配置)】
  • Vue开发系列——Vue中常见实现区别及Vue.js 模板编译原理
  • EC800GCN 华系列 DTU 开发板介绍
  • 基于 Flink+Paimon+Hologres 搭建淘天集团湖仓一体数据链路
  • 家政小程序开发,开启便捷生活新篇章
  • Visual Studio 的下载安装
  • 常用 Linux 命令---服务器开发和运维相关命令
  • JVM内存溢出:诊断处理与预防全攻略
  • 【pycharm】如何连接远程仓库进行版本管理(应用版本)
  • ModbusTcp协议
  • LiveQing 视频点播流媒体 RTMP 推流服务功能:搭建 RTMP 视频流媒体服务详细指南
  • xcode卡死问题,无论打开什么程序xcode总是在转菊花,重启电脑,卸载重装都不行