My solution to an error on rust-analyzer extension.

Error Environment

Windows 10 20H2
VS Code
cd $(rustc --print sysroot); cd lib\rustlib\src\rust => src, Cargo.lock

Error

rust-analyzer failed to load workspace: Failed to find sysroot for Cargo.toml file /*path-to-my-workspace*. Is rust-src installed?: can't load standard library from sysroot /*my-sysroot* (discovered via `rustc --print sysroot`) try installing the Rust source the same way you installed rustc

This solution might be effective to the build failed error as well.

Solution (at your own risk)

Use latest toolchain.

rustup update
rustup default stable

If rustc says override bruh, see also https://shiotomat.hatenablog.com/entry/2022/01/30/135559. You can choose nightly instead.

Rename src at $(rustc --print sysroot)\lib\rustlib\src\rust library, then rust-analyzer works.
I have no idea what will happen after updating rust in the future.

Additional Comment

rustup component add rust-src arises info: component 'rust-src' is up to date

日本語

toolchainが古いと出るようなのでtoolchainを最新のものにすると動くようになります

rustup update
rustup default stable

をします。overrideがなんとかかんとか......と言われた場合、この問題が関係しているかもしれません。
上記エラーが出るときの解決法の一つ。
ls $(rustc --print sysroot)\lib\rustlib\src\rustを実行したときにsrc, Cargo.lockが出てくる人はsrclibraryに名前を変更すると直ります。
アップデート時に問題が起こるかもしれませんがわからないので自己責任で。