Syntax

Rust Define a Trait Syntax

In Rust, define a trait: describes shared behavior — trait Area { fn area(&self) -> f64; }

Quick Answer

Syntax
trait Area { fn area(&self) -> f64; }
RustSyntax

Define a Trait

Code example
trait Area { fn area(&self) -> f64; }

Describes shared behavior.

Learn More

How to define a trait 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 a Trait Syntax

Use trait Area { fn area(&self) -> f64; }.

Related Syntax References

Frequently asked questions

What is the Rust syntax for define a trait?

Use this pattern: trait Area { fn area(&self) -> 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