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?
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;
}
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.