Expanding Rust Lifetime

做~自己de王妃 提交于 2019-12-05 01:06:05

The lifetime of out is not 'a, since out is destroyed at the end of the function. Rust will not allow you to return a reference to it (it would allow accessing freed memory!).

Try changing your function to the following:

fn take_symbol<'a>(ch: &'a str, current: &'a mut String) -> TokenList<'a> {
    let out = TokenList::<'a>::new();

    out.push(current.as_str());
    out.push(ch);

    *current = String::new();

    out
}

This way you will pass the ownership of out to the caller and it will live long enough.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!