0

好的,这是我的 MCVE,马上开始。

fn do_something (string: &'static str) -> Result<&str, isize> {
    Ok(string)
}

fn main() {
    let place = Some("hello".to_string());
    match place {
        Some(input) => {
            let place = &input[..];
            let something = do_something(place);
        }
        _ => (),
    }
}

我似乎想不出满足的方法do_something。在我的实际代码中,do_something是一个库函数,所以我无法更改它的签名。

- 谢谢

4

1 回答 1

0

如果您无法更改函数的签名,那么您需要使用字符串文字来创建&'static str或泄漏内存。

即要么这样做:

do_something("hello");

或者这个(坏主意,可能会坏,只适用于每晚):

let place = Some("hello".to_string());
if let Some(s) = place {
    do_something(unsafe { std::mem::transmute(s.into_boxed_str()) });
}
于 2015-10-08T16:02:45.217 回答