Jihyun
Jihyun

Reputation: 1105

Why can't I call Global.alloc_zeroed()

The type std::alloc::Global is supposed to have the AllocRef trait, which has alloc_zeroed(layout) pub method. But I can't use it .... I ran "rustup override add nightly" to use a nightly. It seems that AllocRef trait is not available in here but I don't know how to make it visible... Please help!

Thank you! I ran "rustup override set nightly" but "rustup show" output did not change and it still failed to compile.. Added "rustup show" output at the end.

PS. After running "rustup override set nightly-2020-01-09-x86_64-apple-darwin" and adding "use std::alloc::Alloc", I see it got compiled! When I ran "rustup override set nightly-x86_64-apple-darwin", it did not compile..

My code:

#![feature(allocator_api)]
use std::alloc::{Layout,Global,Alloc};  // added "Alloc"
fn main() {
    unsafe {
        let layout = Layout::new::<i32>();
        println!("{:?}", Global.alloc_zeroed(layout));
    }
}

Compile error:

error[E0599]: no method named `alloc_zeroed` found for struct `std::alloc::Global` in the 
current scope
 --> src/main.rs:6:26
  |
6 |     println!("{:?}", Global.alloc_zeroed(layout));
  |                             ^^^^^^^^^^^^ method not found in `std::alloc::Global`
error: aborting due to previous error

rustup show:

Default host: x86_64-apple-darwin
rustup home:  /Users/user1/.rustup

installed toolchains
--------------------

stable-x86_64-apple-darwin (default)
nightly-2020-01-09-x86_64-apple-darwin
nightly-x86_64-apple-darwin

active toolchain
----------------

nightly-x86_64-apple-darwin (directory override for '/Users/user1/as/rust22')
rustc 1.51.0-nightly (1d0d76f8d 2021-01-24)

Upvotes: 1

Views: 224

Answers (2)

Gr&#233;gory OBANOS
Gr&#233;gory OBANOS

Reputation: 1051

The Alloc trait have been renamed Allocator & alloc_zeroed have been renamed allocate_zeroed.

Link to nightly docs

Link to playground

Upvotes: 1

Henry
Henry

Reputation: 1633

I think the issue is that you are still on the stable toolchain. rustup override add nightly doesn't switch to nightly it just installs it. Run rustup override set nightly to switch to the nightly toolchain. Then run rustup show to see what your current toolchain is.

Upvotes: 2

Related Questions