ragingSloth
ragingSloth

Reputation: 1104

How do I derive Copy when using #![no_std]?

I am working on a project that uses #![no_std], and I would like to be able to derive useful traits such as Copy and Clone. I tried adding pub use core::prelude::*; to both the project root, and the file I actually want to use it in. However, any attempts to #[derive(Copy)] results in

error: attempt to implement a nonexistent trait std::marker::Copy

I don't understand what I'm doing wrong. Attempting to add

use core::marker::Copy

yields this:

error: a type named Copy has already been imported in this module

Upvotes: 3

Views: 781

Answers (2)

Vladimir Matveev
Vladimir Matveev

Reputation: 128131

You can always implement marker traits with impl:

impl Copy for MyStruct {}

It will work only for marker traits though, Clone and other similar traits still need deriving.

Upvotes: 3

ragingSloth
ragingSloth

Reputation: 1104

#[derive] is broken in #![no_std] pub mod std {pub use core::*;} will fix it, it replaces instances of ::std:: with ::core:: allowing you to derive traits that the compiler thinks are in std from core

Upvotes: 3

Related Questions