How to convert PathBuf to String in Rust: complete guide
Hello colleagues! I recently started learning Rust and encountered a problem when working with the file system. I need to get the absolute path to a file and then save it as a regular string for further processing (for example, passing to JSON or logging).\n\nHere is my code:\n\n```rust\nuse std::path::PathBuf;\nuse std::env;\n\nfn main() {\n let mut path = env::current_dir().unwrap();\n path.push("config.toml");\n \n // Want to get a string, but can't\n let path_str: String = path; // Error!\n println!("Path: {}", path_str);\n}\n```\n\nThe compiler gives an error: `expected String, found PathBuf`. I understand that `PathBuf` and `String` are different types, but how do I convert them? I tried the `.to_str()` method, but it returns `Option<&str>`, and I need exactly a `String`.