Can't create Path from dynamic string?

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/backend.rs:38:1
   |
38 | / pub fn generate_preferred_uds_path() -> Result<std::path::Path, std::env::VarError> {
39 | |     Ok(*std::path::Path::new(&std::env::var("XDG_RUNTIME_DIR")?))
40 | | }
   | |_^ borrow the `Path` instead
   |
   = help: within `std::path::Path`, the trait `std::marker::Sized` is not implemented for `[u8]`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required because it appears within the type `std::path::Path`
   = note: required by `std::result::Result`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `events`.

I don't understand this error message, of course, the size of [u8] is a runtime variable. How can I create a path in runtime with String?

use std::path::PathBuf;

pub fn generate_preferred_uds_path() -> Result<PathBuf, std::env::VarError> {
    let mut path = PathBuf::new();
    path.push(std::env::var("XDG_RUNTIME_DIR")?);
    Ok(path)
}

Path is meant for temporary values, use PathBuf for owned values. This is similar to the difference between [T] and Vec<T>.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.