When I write a simple code for bare metal without using libcore I get the following error:
error: binary operation
!=
cannot be applied to typeu32
[E0369]
Straight forward implementation confronts the chicken-and-egg problem:
#![crate_type = "lib"]
#![feature(no_std, no_core, lang_items)]
#![no_std]
#![no_core]
#[lang = "sized"]
pub trait Sized {}
#[lang = "sync"]
pub trait Sync {}
pub const CONST1: u32 = 1;
pub const CONST2: u32 = 2;
pub struct Struct {
pub field: u32,
}
impl Sync for Struct {}
pub static VAR: Struct = Struct {
field: CONST1 + CONST2,
};
Here I get the following error:
error: binary operation
+
cannot be applied to typeu32
[E0369]
This is actually the expected behavior nowadays. Rust requires the "add" lang item to be implemented for the type, even if it is a built-in type. The implementation can be completely bogus (as long as it compiles), since it will be replaced by the built-in operation.
The reason this is done this way is that the code type-checking the built-in types was buggy and complex. Doing it this way simplified the type-checking code and the trait implementations were already there anyway.
Therefor you need to add the following two declarations to be able to use the +
operator even on the built-in type u32
#[lang = "add"]
pub trait Add<RHS=Self> {
type Output;
fn add(self, rhs: RHS) -> Self::Output;
}
impl Add for u32 {
type Output = u32;
fn add(self, _rhs: u32) -> u32 { 42 }
}
Here's a full example that doesn't link since it's missing some libc stuff: https://play.rust-lang.org/?gist=a223d48b0f2d8533996f&version=nightly
Note that you should NOT create a bogus implementation as shown, because you might be using the built-in type in a generic context, where the implementation will actually be used
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With