Error message from cargo: ld.so: rustc: can't load library

This had me stumped for a while so I am posting here in case it helps someone else via a search.

After some glitches with my installation of rust and rust-src (on OpenBSD), which I resolved by removing some older manual copies of files and just un/re-installing those packages, I got this error message when running, for example cargo check or cargo clean on a "hello world" that I had built before:

$cargo clean
error: process didn't exit successfully: rustc -vV (signal: 9, SIGKILL: kill)
--- stderr
ld.so: rustc: can't load library 'libstd-02e247769dc17415.so'

The problem didn't occur with a new project created by cargo.

The solution was to delete the project's target directory.

It also had caused errors in my ~/.local/state/nvim/lsp.log file and problems with using rust-analyzer (LSP). Here are those messages in case someone searches for them:

[ERROR][2025-01-15 12:08:24] .../vim/lsp/rpc.lua:770 "rpc" "/home/lacall/bin/rust-analyzer" "stderr" "2025-01-15T19:08:24.618444Z ERROR failed to get rustc cfgs e=unable to fetch cfgs via rustc --print cfg -O\n\nCaused by:\n "/usr/local/bin/rustc" "--print" "cfg" "-O" failed, signal: 9 (SIGKILL)\n stderr:\n ld.so: rustc: can't load library 'libstd-02e247769dc17415.so'\n \n\nStack backtrace:\n 0: anyhow::error::::msg\n 1: project_model::utf8_stdout\n 2: project_model::rustc_cfg::get\n 3: project_model::workspace::ProjectWorkspace::load\n 4: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::fold\n 5: <alloc::vec::Vec as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter\n 6: core::ops::function::FnOnce::call_once{{vtable.shim}}\n 7: std::sys::backtrace::__rust_begin_short_backtrace\n 8: core::ops::function::FnOnce::call_once{{vtable.shim}}\n 9: std::sys::pal::unix::thread::thread::new::thread_start\n 10: _rthread_start\n at /usr/src/lib/librthread/rthread.c:96:11\n 11: __tfork_thread\n at /usr/src/lib/libc/arch/amd64/sys/tfork_thread.S:87\n"
[ERROR][2025-01-15 12:08:24] .../vim/lsp/rpc.lua:770 "rpc" "/home/lacall/bin/rust-analyzer" "stderr" "2025-01-15T19:08:24.679453Z ERROR failed fetching data layout for ManifestPath { file: AbsPathBuf("/home/lacall/proj/om/core/Cargo.toml") } workspace e=RUSTC_BOOTSTRAP="1" "/usr/local/bin/rustc" "-Z" "unstable-options" "--print" "target-spec-json" failed, signal: 9 (SIGKILL)\nstderr:\nld.so: rustc: can't load library 'libstd-02e247769dc17415.so'\n\n"
[ERROR][2025-01-15 12:08:25] .../vim/lsp/rpc.lua:770 "rpc" "/home/lacall/bin/rust-analyzer" "stderr" "2025-01-15T19:08:25.099355Z ERROR Flycheck failed to run the following command: CommandHandle { program: "/usr/local/bin/cargo", arguments: ["check", "--workspace", "--message-format=json", "--manifest-path", "/home/lacall/proj/om/core/Cargo.toml", "--keep-going", "--all-targets"], current_dir: Some("/home/lacall/proj/om/core") }, error=Cargo watcher failed, the command produced no valid metadata (exit code: ExitStatus(unix_wait_status(25856))):\nerror: process didn't exit successfully: rustc -vV (signal: 9, SIGKILL: kill)\n--- stderr\nld.so: rustc: can't load library 'libstd-02e247769dc17415.so'\n\n\n"

(Marking this as solved: manually delete the target directory and re-run whatever cargo or other command.)