Why does my variable not live long enough?

孤街浪徒 提交于 2019-11-27 02:17:55

The problem is that string is created in your function and will be destroyed when the function returns. The vector you want to return contains slices of string, but those will not be valid outside of your function.

If you're not terribly worried about performance, you can return a Vec<String> from your function. You just have to return type to Result<Vec<String>, io::Error> and change the line

let data: Vec<&str> = string.split('\n').collect();

to

let data: Vec<String> = string.split('\n').map(String::from).collect();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!