How to use associated functions in defining associated constants


the following doesn't work

struct Struct;

impl Struct {
   fn five(input: &str) -> i32 {5}
    const N: i32 = five("give me five");

the compiler error is cannot find function five in this scope

what's the right way to use associated functions in the definition of associated constants?

struct Struct;

impl Struct {
    const fn five(input: &str) -> i32 { 5 }
    const N: i32 = Self::five("give me five");
1 Like

@quinedot what should I do if I have to use ? in the function definition?
The return type of my function is a Result<>
now I got

`?` is not allowed in a `const fn`

Some control flow is in stable now, but not ?. You try can unrolling the match or similar:

        let value = match foo {
            // you may have to map or coerce the error in addition
            Err(err) => return Err(err),
            Ok(val) => val,

n.b. E0744 appears somewhat outdated, I'll file a bug.

1 Like

(A documentation fix is already in the pipeline.)


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.