Question about constants

I want to have a constant that is meant to represent 100% but seemingly not doable in Rust

static HUNDRED_PERCENT: &'static Decimal = &Decimal::new(100, 0);

Is there really no way to do this in Rust?

error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
  --> src/asset_allocation.rs:15:45
   |
15 | static HUNDRED_PERCENT: &'static Decimal = &Decimal::new(100, 0);
   |

Assuming you’ve defined a custom struct to hold your decimal values - there is.

You can look at it over here.

Add the & if you need it if you absolutely need a reference to your struct instead of the struct itself.

static HUNDRED_PERCENT: &Decimal = &Decimal(100, 0);

You might want to get into the habit of checking the Rust Compiler Error Index.

The problem you got was due to the fact that you were trying to call a run-time function (which would be evaluated while the program is running otherwise) at the time of compilation, which Rust doesn’t support.

P. S. You might want to KISS a bit more (Keep It Simple, Stupid / Keep It Stupidly Simple).

Yes i have tried to look at the error but seems like its not helping.

use rust_decimal::Decimal;
const HUNDRED_PERCENT: Decimal = Decimal::new(100, 0);

I don’t understand why this does not work. When in this RFC https://github.com/rust-lang/rfcs/blob/master/text/0911-const-fn.md it has a example

const ORIGIN: Point = Point::new(0, 0);

Your function (as the error states) must be const, e.g.

pub const fn new(a: u8, b: u8) -> Decimal

It works perfectly