在rust中将Vec <&str>转换为Vec <&CStr>

时间:2020-01-05 17:16:33

标签: rust lifetime borrow-checker borrow

看看这个功能:

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);
}

它需要一个Vec<&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

如何解决此错误?

1 个答案:

答案 0 :(得分: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