How do I access other variables inside impl?

Hi I am having a bit of problem with accessing a variable inside impl.

struct AllPaths
{
    userprofile             : PathBuf,
    dir_config_path         : PathBuf,
    file_config             : PathBuf,
}
impl AllPaths
{
    // Initializes
    fn init() -> Self
    {
        Self
        {
            userprofile:
            {
                let userprofile = Command::new("cmd.exe")
                .arg("/c")
                .arg("echo")
                .arg("%userprofile%")
                .output()
                .expect("Failed to execute command!");
            
                let mut userprofile = String::from_utf8_lossy(&userprofile.stdout).to_string();
                
                // Removes the enter and the carriage return characters
                for _ in 0..2
                {
                    userprofile.pop();
                }
                PathBuf::from(userprofile)
            },
            
            dir_config_path:
            {
                let mut dir_config_path = PathBuf::from(userprofile.display().to_string()); // Error with userprofile
                dir_config_path.push(r"Important");
                PathBuf::from(dir_config_path)
            },

            file_config: PathBuf::from("startup_configuration.txt"),
        }
    }
}

How do I access the value of userprofile? in this line:

let mut dir_config_path = PathBuf::from(userprofile.display().to_string()); there is an error and I am not too sure how to access userprofile?

With the current structure of your code, you can't. You need to do it before you start the Self { ... } expression.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.