Skip to content

make functions const where possible #117

Open
@rursprung

Description

@rursprung

as the title says: it'd be great if as many functions as possible were made const so that they can be used in a const environment.

currently this code will not compile because the Add implementation of Complex isn't const:

use num::complex::Complex32;

const X: Complex32 = Complex32::new(1., 0.);
const Y: Complex32 = Complex32::new(0., 1.);
const Z: Complex32 = X + Y;

you can find the same example on the playground.

error from the playground:

   Compiling playground v0.0.1 (/playground)
error[E0015]: cannot call non-const operator in constants
   --> src/main.rs:6:26
    |
6   |     const Z: Complex32 = X + Y;
    |                          ^^^^^
    |
note: impl defined here, but it is not `const`
   --> /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/num-complex-0.4.4/src/lib.rs:724:1
    |
724 | impl<T: Clone + Num> Add<Complex<T>> for Complex<T> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: calls in constants are limited to constant functions, tuple structs and tuple variants

For more information about this error, try `rustc --explain E0015`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions