Syntax

Rust If Expression Syntax

In Rust, if expression: if returns a value — let label = if n > 0 { "pos" } else { "neg" };

Quick Answer

Syntax
let label = if n > 0 { "pos" } else { "neg" };
RustSyntax

If Expression

Code example
let label = if n > 0 { "pos" } else { "neg" };

If returns a value.

Learn More

How to if expression in Rust

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

Rust If Expression Syntax

Use let label = if n > 0 { "pos" } else { "neg" };.

Related Syntax References

Frequently asked questions

What is the Rust syntax for if expression?

Use this pattern: let label = if n > 0 { "pos" } else { "neg" };

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