Rust: CString、CStr和String、str

news2025/6/5 16:30:14

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

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

一、CString、String等代码探析

因为有用到外部libc库,所以:

1、toml文件


[dependencies]
libc = "0.2"

2、相关代码

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")
}
fn cstr_to_cstring(s:&CStr) ->CString{
    s.to_owned()
}
// *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
}
//C中分配内存,C中释放内存
fn c_allocate_for_rust_as_cstring(){
    #[allow(unused_extern_crates)]
    extern crate libc; //toml文件中,需要添加libc = "0.2"
    // 分配内存来存储一个字符串
    let size = 12; // 例如,分配足够存储 "Hello, world" 的内存
    let ptr = unsafe { libc::malloc(size) as *mut c_char };
 
    if !ptr.is_null() {
        // 使用 C 字符串格式填充内存
        let c_str = CString::new("Hello, world").unwrap();
        unsafe{
            std::ptr::copy_nonoverlapping(c_str.as_ptr(), ptr, size);
        }
         // 使用 CStr 来安全地访问这个内存
        let rust_str = unsafe { CStr::from_ptr(ptr) };
        println!("{}", rust_str.to_str().unwrap());
        unsafe{
            // 释放内存
            libc::free(ptr as *mut _);
        }
    } else {
        println!("Memory allocation failed");
    }
}

二、输出结果

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

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2397409.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

力扣每日一题——连接两棵树后最大目标节点数目 ||

目录 题目链接&#xff1a;3373. 连接两棵树后最大目标节点数目 II - 力扣&#xff08;LeetCode&#xff09; 题目描述 解法一&#xff1a;​​双树贡献分离法​​ Java写法&#xff1a; C写法&#xff1a; 运行时间 时间复杂度和空间复杂度 总结 题目链接&#xff1a;…

【学习笔记】Sparse Crosscoders for Cross-Layer Features and Model Diffing

Sparse Crosscoders for Cross-Layer Features and Model Diffing Abstract 本说明介绍了稀疏跨编码器(sparse crosscoders)&#xff0c;它是一种稀疏自编码器(sparse autoencoders)或transcoders的变体&#xff0c;旨在用于理解叠加中的模型结构。SAEs是在单一层中编码和预测…

VSCode无法转到定义python源码(ctrl加单击不跳转)

已经尝试的方案&#xff1a; 1.确保对应python环境正确激活 在 VSCode 中&#xff0c;打开命令面板&#xff08;CtrlShiftP&#xff09;&#xff0c;输入并选择 Python: Select Interpreter&#xff0c;然后从列表中选择正确的 Python 解释器。 2.重新卸载Python插件再重新安装…

【华为战报】4月、5月 HCIP考试战报!

了解更多往期考试→点 【考试战报】 华为认证 HCIP 4、5月微思 | HCIP 考试战报 学员成绩单 华为认证 最新开班 厦门面授 全国直播 新生代网工必看&#xff1a;华为模拟器eNSP安装教程&#xff08;附下载链接&#xff09;

AIGC工具平台-GPT-SoVITS-v4-TTS音频推理克隆

声音克隆与语音合成的结合&#xff0c;是近年来生成式AI在多模态方向上的重要落地场景之一。随着预训练模型能力的增强&#xff0c;结合语音识别、音素映射与TTS合成的端到端系统成为初学者可以上手实践的全流程方案。 围绕 GPT-SoVITS-v4-TTS 模块&#xff0c;介绍了其在整合…

el-table配置表头固定而且高度变化

根据官网提示只要在 el-table 元素中定义了 height 属性&#xff0c;即可实现固定表头的表格&#xff0c;而不需要额外的代码。 如果你想既要固定表头&#xff0c;又要下方表格高度自适应&#xff0c;可以设置为 height"100%" &#xff1a; 然后外层设置scroll:

设计模式——组合设计模式(结构型)

摘要 组合设计模式是一种结构型设计模式&#xff0c;用于将对象组合成树形结构以表示“部分-整体”的层次结构&#xff0c;使客户端对单个对象和组合对象具有一致的访问方式。它包含抽象组件、叶子节点和组合节点&#xff0c;具有统一处理、支持递归结构和易扩展等优点&#x…

EMO2:基于末端执行器引导的音频驱动虚拟形象视频生成

今天带来EMO2&#xff08;全称End-Effector Guided Audio-Driven Avatar Video Generation&#xff09;是阿里巴巴智能计算研究院研发的创新型音频驱动视频生成技术。该技术通过结合音频输入和静态人像照片&#xff0c;生成高度逼真且富有表现力的动态视频内容&#xff0c;值得…

Python打卡训练营Day43

DAY 43 复习日 作业&#xff1a; kaggle找到一个图像数据集&#xff0c;用cnn网络进行训练并且用grad-cam做可视化 数据集地址&#xff1a;Lung Nodule Malignancy 肺结核良恶性判断 进阶&#xff1a;并拆分成多个文件 import os import pandas as pd import numpy as np from…

PHP7+MySQL5.6 查立得轻量级公交查询系统

# PHP7MySQL5.6 查立得轻量级公交查询系统 ## 系统简介 本系统是一个基于PHP7和MySQL5.6的轻量级公交查询系统(40KB级)&#xff0c;支持线路查询、站点查询和换乘查询功能。系统采用原生PHPMySQL开发&#xff0c;无需第三方框架&#xff0c;适合手机端访问。 首发版本&#x…

如何做好一个决策:基于 Excel的决策树+敏感性分析应用(针对多个变量)

本文是对《如何做好一个决策:基于 Excel的决策树+敏感性分析应用》一文的补充。 示例背景 决策问题:是否开发新产品? 关键变量: 开发成本(B2):$500K, $700K, $1M高需求概率(B4):30%, 50%, 70%高需求收入(C4

Azure DevOps 管道部署系列之一本地服务器

Azure DevOps 是一个帮助改进 SDLC(软件开发生命周期)的平台。 在本文中,我们将使用 Azure Pipelines 创建自动化部署。 Azure DevOps 团队将 Azure Pipelines 定义为“使用 CI/CD 构建、测试和部署,适用于任何语言、平台和云平台”。 在这里,我将解释如何在 Azure Dev…

Celery简介

一、什么是异步任务队列 异步任务队列是指一种用于管理和调度异步执行任务的机制。具体来说&#xff0c;它允许将任务放入队列中&#xff0c;然后由后台进程异步处理这些任务&#xff0c;而不会阻塞主线程的执行。这种设计使得系统能够高效地处理耗时操作&#xff0c;同时保持…

基于 GitLab CI + Inno Setup 实现 Windows 程序自动化打包发布方案

在 Windows 桌面应用开发中&#xff0c;实现自动化构建与打包发布是一项非常实用的工程实践。本文以我在开发PackTes项目时的为例&#xff0c;介绍如何通过 GitLab CI 配合 Inno Setup、批处理脚本、Qt 构建工具&#xff0c;实现版本化打包并发布到共享目录的完整流程。 项目地…

web架构2------(nginx多站点配置,include配置文件,日志,basic认证,ssl认证)

一.前言 前面我们介绍了一下nginx的安装和基础配置&#xff0c;今天继续来深入讲解一下nginx的其他配置 二.nginx多站点配置 一个nginx上可以运行多个网站。有多种方式&#xff1a; http:// ip/域名 端口 URI 其中&#xff0c;ip/域名变了&#xff0c;那么网站入口就变了…

AI 的早期萌芽?用 Swift 演绎约翰·康威的「生命游戏」

文章目录 摘要描述题解答案题解代码分析示例测试及结果时间复杂度空间复杂度总结 摘要 你有没有想过&#xff0c;能不能通过简单的规则模拟出生与死亡&#xff1f;「生命游戏」正是这样一种充满魅力的数学模拟系统。这篇文章我们来聊聊它的规则到底有多神奇&#xff0c;并用 S…

go|channel源码分析

文章目录 channelhchanmakechanchansendchanrecvcomplieclosechan channel 先看一下源码中的说明 At least one of c.sendq and c.recvq is empty, except for the case of an unbuffered channel with a single goroutine blocked on it for both sending and receiving usin…

【大模型学习】项目练习:视频文本生成器

&#x1f680;实现视频脚本生成器 视频文本生成器 &#x1f4da;目录 一、游戏设计思路二、完整代码解析三、扩展方向建议四、想说的话 一、⛳设计思路 本视频脚本生成器采用模块化设计&#xff0c;主要包含三大核心模块&#xff1a; 显示模块&#xff1a;处理用户输入和…

【Rust】Rust获取命令行参数以及IO操作

✨✨ 欢迎大家来到景天科技苑✨✨ &#x1f388;&#x1f388; 养成好习惯&#xff0c;先赞后看哦~&#x1f388;&#x1f388; &#x1f3c6; 作者简介&#xff1a;景天科技苑 &#x1f3c6;《头衔》&#xff1a;大厂架构师&#xff0c;华为云开发者社区专家博主&#xff0c;…

【Redis】Zset 有序集合

文章目录 常用命令zaddzcardzcountzrange && zrevrangezrangebyscorezpopmax && bzpopmaxzpopmin && zpopmaxzrank && zrevrankzscorezremzremrangebyrankzremrangebyscorezincrby 集合间操作交集 zinterstore并集 zunionstore 内部编码应用场…