The Rust language can also attach functions to enums through impl keyword (for implementation) https://doc.rust-lang.org/std/keyword.impl.html
Or can I define the procedures outside of the type declaration and have it work effectively the same way? I don't quite know how I would do that though.
Enums are just like any other type. You can "attach" procs to them like you can any other type.
Here's the equivalent of that Python enum code:
import std/strformat
type
Mood = enum
Funky = 1
Happy = 3
func describe(mood: Mood): (string, int) =
($mood, mood.ord)
func `$`(mood: Mood): string =
&"my custom str! {mood.ord}"
func favoriteMood(_: typedesc[Mood]): Mood =
Happy
when isMainModule:
assert Mood.favoriteMood == Happy
echo Happy.describe
echo Funky
Everything is covered in the tutorials and the manual.
Declaring it outside the type is what you would do.
I'm a bit uncertain what you are looking for, but this kinda translates the two kinds of calls you have in the python example
type
Colour = enum
Red
Green
Blue
proc someColour(x: Colour) =
echo "This is " & $x
# That is called on a value of the enum
echo Red.someColour()
proc something(x: typedesc[Colour]) =
echo "This is colour"
# This is called on the type (Kinda like a static method)
echo Colour.something()