首页 > 解决方案 > 在 rust 中将 Vec<&str> 转换为 Vec<&CStr>

问题描述

看看这个函数:

fn exec(cli: Vec<&str>) {
    eprintln!("execing: {:?}", cli);
    let args: Vec<&CStr> = cli.iter()
        .map(|s| CString::new(s.as_bytes()).unwrap().as_c_str())
        .collect();
    execv(args[0], &args);
    debug(args);
}

它接受 aVec<&str>并将其作为命令执行。我无法将其转换为Vec<&CStr>(这是execv需要的)。编译器针​​对以下操作报告此错误map

error[E0515]: cannot return value referencing temporary value
   --> src/idea.rs:141:18
    |
141 |         .map(|s| CString::new(s.as_bytes()).unwrap().as_c_str())
    |                  -----------------------------------^^^^^^^^^^^
    |                  |
    |                  returns a value referencing data owned by the current function
    |                  temporary value created here

如何修复此错误?

标签: rustlifetimeborrow-checkerborrow

解决方案


您必须将所有 CString 收集到一个单独的向量中,以便您的引用在 execv 调用期间有效:

use std::ffi::CString;
use std::ffi::CStr;

fn main() {
    let cli = vec!["hello", "world"];
    let vec: Vec<_> = cli.iter()
        .map(|s| CString::new(s.as_bytes()).unwrap())
        .collect();
    let vec_obj: Vec<&CStr> = vec.iter().map(|c| c.as_c_str()).collect();
    println!("CString:{:?}", vec);
    println!("&CStr:{:?}", vec_obj);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c440ea898abe2ed5573993923ee6b74f


推荐阅读