Syntax

Rust Define an Enum Syntax

In Rust, define an enum: defines a type with variants — enum Shape { Circle(f64), Square(f64), }

Quick Answer

Syntax
enum Shape { Circle(f64), Square(f64), }
RustSyntax

Define an Enum

Code example
enum Shape { Circle(f64), Square(f64), }

Defines a type with variants.

Learn More

How to define an enum in Rust

  1. Open your Rust file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Rust Define an Enum Syntax

Use enum Shape { Circle(f64), Square(f64), }.

Related Syntax References

Frequently asked questions

What is the Rust syntax for define an enum?

Use this pattern: enum Shape { Circle(f64), Square(f64), }

Can I copy this Rust code example?

Yes. Use Copy code and adapt the example to your program.

Is this Rust syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify