2

看看这个函数:

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

如何修复此错误?

4

1 回答 1

2

您必须将所有 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

于 2020-01-05T23:38:20.683 回答