Reputation: 4009
In standard Rust code, the vec!
macro is in the prelude and there is no need to make it visible manually. I'm working on a library that doesn't use the standard library and sets #![no_std]
, so also the prelude isn't visible.
Inside the test code, I am using functionality from the standard library, therefore I have a
#[cfg(test)]
extern crate std;
This works without problems to access functions and datatypes from the standard library, but now I would like to access the vec!(...)
macro and I don't know how.
use std::vec::vec!;
results in an error:
expected one of `::`, `;`, or `as` here
at the position of the exclamation mark.
How can I access this macro instead?
Upvotes: 15
Views: 9367
Reputation: 13460
In addition to Tims answer, if you have an embedded system and you have an allocator, but not std, you can use
#[macro_use]
extern crate alloc;
to be able to use vec!
Upvotes: 12
Reputation: 8486
vec!
is a macro so you have to add #[macro_use]
:
#[cfg(test)]
#[macro_use]
extern crate std;
If you are on a nightly compiler, you can also use the use_extern_macros feature:
#![feature(use_extern_macros)]
#[cfg(test)]
extern crate std;
#[cfg(test)]
mod test {
use std::vec;
}
Upvotes: 3