How to convert the PathBuf to String

前端 未结 3 1937
温柔的废话
温柔的废话 2020-12-15 03:31

I have to convert the PathBuf variable to a String to feed my function. My code is like this:

let cwd = env::current_dir().unwrap()         


        
3条回答
  •  Happy的楠姐
    2020-12-15 04:11

    As mcarton already say is not so simple and not all path are UTF-8 encoded. But you can use:

    p.into_os_string().into_string()
    

    To have a fine control of it. By ? you can send error to upper level or simply ignore it by unwrap():

    let my_str = cwd.into_os_string().into_string().unwrap();
    

    The beauty thing about into_string() is that the error wrap the original OsString value.

提交回复
热议问题