Repository: starship/starship
File: src/modules/rust.rs (Rust)

Code before:

    Some(module)
}

fn get_module_version(context: &Context, config: &RustConfig) -> Option<String> {
    // `$CARGO_HOME/bin/rustc(.exe) --version` may attempt installing a rustup toolchain.
    // https://github.com/starship/starship/issues/417
    //
    // To display appropriate versions preventing `rustc` from downloading toolchains, we have to
    // check
    // 1. `$RUSTUP_TOOLCHAIN`
    // 2. `rustup override list`
    // 3. `rust-toolchain` or `rust-toolchain.toml` in `.` or parent directories
    // 4. `rustup default`
    // as `rustup` does.
    // https://github.com/rust-lang/rustup.rs/tree/eb694fcada7becc5d9d160bf7c623abe84f8971d#override-precedence
    //
    // Probably we have no other way to know whether any toolchain override is specified for the
    // current directory. The following commands also cause toolchain installations.
    // - `rustup show`
    // - `rustup show active-toolchain`
    // - `rustup which`
    if let Some(toolchain) = env_rustup_toolchain(context)
        .or_else(|| execute_rustup_override_list(&context.current_dir))
        .or_else(|| find_rust_toolchain_file(context))
        .or_else(execute_rustup_default)
    {
        match execute_rustup_run_rustc_version(&toolchain) {
            RustupRunRustcVersionOutcome::RustcVersion(rustc_version) => {
                format_rustc_version(&rustc_version, config.version_format)
            }
            RustupRunRustcVersionOutcome::ToolchainName(toolchain) => Some(toolchain),
            RustupRunRustcVersionOutcome::RustupNotWorking => {
                // If `rustup` is not in `$PATH` or cannot be executed for other reasons, we can
                // safely execute `rustc --version`.
                format_rustc_version(&execute_rustc_version()?, config.version_format)
            }
            RustupRunRustcVersionOutcome::Err => None,
        }
    } else {
        format_rustc_version(&execute_rustc_version()?, config.version_format)
    }
}

fn env_rustup_toolchain(context: &Context) -> Option<String> {
    let val = context.get_env("RUSTUP_TOOLCHAIN")?;
    Some(val.trim().to_owned())
}

fn execute_rustup_override_list(cwd: &Path) -> Option<String> {
    let Output { stdout, .. } = create_command("rustup")

Review comment (suggestion):
Related to the below suggestion
```suggestion
        .or_else(|| execute_rustup_default(context))
```

Code after:

    Some(module)
}

fn get_module_version(context: &Context, config: &RustConfig) -> Option<String> {
    // `$CARGO_HOME/bin/rustc(.exe) --version` may attempt installing a rustup toolchain.
    // https://github.com/starship/starship/issues/417
    //
    // To display appropriate versions preventing `rustc` from downloading toolchains, we have to
    // check
    // 1. `$RUSTUP_TOOLCHAIN`
    // 2. `rustup override list`
    // 3. `rust-toolchain` or `rust-toolchain.toml` in `.` or parent directories
    // 4. `rustup default`
    // as `rustup` does.
    // https://github.com/rust-lang/rustup.rs/tree/eb694fcada7becc5d9d160bf7c623abe84f8971d#override-precedence
    //
    // Probably we have no other way to know whether any toolchain override is specified for the
    // current directory. The following commands also cause toolchain installations.
    // - `rustup show`
    // - `rustup show active-toolchain`
    // - `rustup which`
    if let Some(toolchain) = env_rustup_toolchain(context)
        .or_else(|| execute_rustup_override_list(context))
        .or_else(|| find_rust_toolchain_file(context))
        .or_else(|| execute_rustup_default(context))
    {
        match execute_rustup_run_rustc_version(&toolchain) {
            RustupRunRustcVersionOutcome::RustcVersion(rustc_version) => {
                format_rustc_version(&rustc_version, config.version_format)
            }
            RustupRunRustcVersionOutcome::ToolchainName(toolchain) => Some(toolchain),
            RustupRunRustcVersionOutcome::RustupNotWorking => {
                // If `rustup` is not in `$PATH` or cannot be executed for other reasons, we can
                // safely execute `rustc --version`.
                format_rustc_version(&execute_rustc_version()?, config.version_format)
            }
            RustupRunRustcVersionOutcome::Err => None,
        }
    } else {
        format_rustc_version(&execute_rustc_version()?, config.version_format)
    }
}

fn env_rustup_toolchain(context: &Context) -> Option<String> {
    let val = context.get_env("RUSTUP_TOOLCHAIN")?;
    Some(val.trim().to_owned())
}

fn execute_rustup_override_list(context: &Context) -> Option<String> {
    extract_toolchain_from_rustup_override_list(