Syntax

Rust Define a Function Syntax

In Rust, define a function: defines a function with a return type — fn add(a: i32, b: i32) -> i32 { a + b }

Quick Answer

Syntax
fn add(a: i32, b: i32) -> i32 { a + b }
RustSyntax

Define a Function

Code example
fn add(a: i32, b: i32) -> i32 { a + b }

Defines a function with a return type.

Learn More

How to define a function 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 Function Syntax

Use fn add(a: i32, b: i32) -> i32 { a + b }.

Related Syntax References

Frequently asked questions

What is the Rust syntax for define a function?

Use this pattern: fn add(a: i32, b: i32) -> i32 { a + b }

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