The trait bound `Number: Clone` is not satisfied

I had written some code for my library, arkley_algebra , around 3 weeks ago. It built successfully, ran without issues, and passed the tests. However, now it's not compiling, and I'm getting an error message that says: "the trait bound Number: Clone is not satisfied ." This is surprising because the trait bound for Clone is implemented using the derive attribute, and it had worked perfectly before without any changes to the code or modifications to the Rust tools version.

The specific error message is as follows:

error[E0277]: the trait bound `Number: Clone` is not satisfied
  --> arkley_algebra\src\term.rs:21:5
   |
18 | #[derive(Debug,PartialEq,Clone)]
   |                          ----- in this derive macro 
expansion
...
21 |     coefficient: Number,
   |     ^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `Number`
   |
   = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `Number: Clone` is not satisfied
  --> arkley_algebra\src\term.rs:24:5
   |
18 | #[derive(Debug,PartialEq,Clone)]
   |                          ----- in this derive macro 
expansion
...
24 |     variables: Variables,
   |     ^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `Number`
   |
   = note: required for `BTreeMap<char, Number>` to implement `Clone`
   = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

Here is the relevant code:

/// Represents a collection of variables, each associated with a numerical value.
/// The `Variables` type is an alias for `BTreeMap<char, Number>`.
pub type Variables = BTreeMap<char,Number>;

/// A struct representing a mathematical term.
///
/// A `Term` is a basic unit in a mathematical expression. It consists of a coefficient
/// (which can be any type that implements the `Numeric` trait) and variables represented
/// as `BTreeMap<char,Number>` .
#[derive(Debug,PartialEq,Clone)]
pub struct Term {
    /// The coefficient of the term.
    coefficient: Number,

    /// The variables and their exponents in the term.
    variables: Variables,
}

Where Number is defined as:

/// Represents a numeric value that can be decimal (aka f64) or Fraction or Standardform number
///
/// `Note` : add fractions variant to is as well 
#[derive(Debug, PartialEq, Clone)]
pub enum Number {
    /// Represents a floating-point decimal number.
    Decimal(f64),
    /// Represents a number in the StandardForm notation.
    StandardForm(StandardForm),
} 
/// Represents a number in standard form.
///
/// The `Standardform` struct holds the significand (mantissa) of the number 
/// and an exponent that determines the power of 10 by which the significand should be multiplied.
#[derive(Debug,PartialEq,Clone)]
pub struct StandardForm  {
    mantissa : f64,
    exponent : i8
}

I have even tried manually implementing the Clone trait for Number , like this:

impl Clone for Number {
    fn clone(&self) -> Self {
        match self {
            Number::Decimal(d) => Number::Decimal(*d),
            Number::StandardForm(sf) => Number::StandardForm(sf.clone()),
        }
    }
}

Despite this effort, I'm still encountering the same error. I'm puzzled because I don't have any other errors that might be interfering with the implementation of the Clone trait, such as macros.

Is there a specific reason for this issue, or is there a way to address and resolve it? I'm unsure of what could be causing this unexpected behavior.

Can you try cleaning cargo's cache (cargo clean) and compile your library from scratch?

In fact that was the first thing I did, since having used gradle , I know the trouble. However even that had no effect on the error during compilation

The code you posted does work for me: Rust Playground

Maybe there is another issue, such as:

  • Some issue elsewhere in the library causing issues?
  • More than one version of the code (copied into different modules and the compiler is looking at the wrong one?)
  • Unsaved files (editor and file system don't match).
  • You switched to an older rust version by mistake, or something like that.
  • I would also try to clean not just the build directory with cargo clean, but the entire download cache (somewhere under ~/.cargo on *nix, I'm on mobile, can't check exact path).
  • Maybe you have corrupt files in your file system in the install of rust itself?

Is ,Number defined in your library? Have you checked that you are importing the correct Number?

1 Like

Some issue elsewhere in the library causing issues?

Firstly that's the only file and code in the library rn and second I have zero other errors

More than one version of the code (copied into different modules and the compiler is looking at the wrong one?)

The Number enum is a local library that is also publish on crates.io under the name of arkley_numerics and no local change besides adding derive clone to it

Unsaved files (editor and file system don't match).

I have done ctrl c many times before asking this question

You switched to an older rust version by mistake, or something like that.

Highly unlikely since I have zero clue how to do it and the only command I have used is cargo

Maybe you have corrupt files in your file system in the install of rust itself?

Unlikely since I have just compiled other rust libs that I made with success after cargo clean

And the other point of the clear cache I will try it

Are you sure you aren't pulling in the published version by accident? That would explain the error

1 Like

No :-1: ,not yet I am using a local path until the library I'm working on works on a basic level so any local changes is updated correctly

IIRC if the version number in your Cargo.toml doesn't match the version in your local path then it will fall back to the published version...

EDIT: definitely double check this. I tried it just now and it didn't cause a problem, it just used the local version; but I swear this caused me problems before.

Plus the cafe I'm in is playing Virtual Insanity by Jamiroquai and I'm wondering if I'm going insane and my future really IS made of...

2 Likes

To be completely safe you aren’t using crates.io, you can also completely remove the version number from Cargo.toml – it isn’t required for a path dependency, anyways

[dependencies]
arkley_numerics = { path = "…" }
1 Like

Yes that was the issue , since i was using arkley_numerics.workspace = true which using version 0.0.2 (in which i forgot to add clone) since then i changed it to arkley_numerics = { path = "../arkley_numerics" } which solved my issue.
Solution : Don't be stupid just check the cargo.toml correctly

1 Like