[solved] Can't find crate for `alloc` in rust

rustc 1.84.0-nightly (662180b34 2024-10-20)

I use the alloc crate in my lib.rs like this:

lib.rs:

#![no_std]
#![cfg_attr(test, no_main)]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]
#![feature(abi_x86_interrupt)]

extern crate alloc;
pub mod allocator;

But when I want to compile, the error occurs:

error[E0463]: can't find crate for `alloc`
 --> src\lib.rs:8:1
  |
8 | extern crate alloc;
  | ^^^^^^^^^^^^^^^^^^^ can't find crate

my part of Cargo.toml:

[unstable]
build-std = ["core", "compiler_builtins", "alloc"]

but allocator.rs did not report an error

use alloc::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut;

pub struct Dummy;

unsafe impl GlobalAlloc for Dummy {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        null_mut()
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        panic!("dealloc should be never called")
    }
}

a minimal reproducible is here : minimal_reproducible

my ask in stackoverflow rust - How can I specify a custom Cargo output directory? - Stack Overflow
How can I fix this, thanks so mush QAQ

How do you compile your code?

Please share any cross-posts you make on other sites, to avoid duplicated effort by the community.

1 Like

Maybe you don't have the rust-src component installed? It is needed by Cargo's unstable build-std feature. What is your target?

Before compiling the code, I ran rustup update nightly and rustup override set nightly , and I believe rust-src has been installed

In your title it now says [solved]. Would you mind sharing what exactly solved your problem (and marking the answer as the solution)? This could be helpful to other people with a similar problem.

I think the problem is they included

[unstable]
build-std = ["core", "compiler_builtins", "alloc"]

in the Cargo.toml when it belongs in .cargo/config.toml or similar.

Their now deleted follow-up post says .cargo/config.toml contains

build-std = ["core", "compiler_builtins"]

which is noticably missing alloc.

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.