Are functions statements?

Rust is expression based language. Its clearly mentioned in the rust book that function definitions are statements and if its a statement , should it not end with ";" ?

fn main() {
println!("hello, world");

} <----- Shouldn't i end with ";" if its a statement?

I am new to rust, could some one explain this ?

According to reference, there are five kinds of statements:

  • A lone semicolon. That's what makes code like x.do_something();; (note the double semicolon) syntactically valid Rust, since the second semicolon is parsed as another statement.
  • An item. There are many different kinds of items, then, each with its own syntactic rules, and functions is just one of them.
  • A let statement. It is, essentially, an expression statement (discussed below) with let pattern = added before.
  • An expression statement. That's where the semicolon comes into play: when we have an expression, it can be turned into statement by adding a semicolon at the end.
  • A macro invocation, with, again, its own set of syntactic rules.

You can see that semicolon is not the property of all statements, but rather of a subset - most notably the expression statements, which are the most common ones.


Certain expression statements that end with {}-braces don't need a semicolon either. This includes if, loop, match, while, if let, for and blocks (including unsafe blocks). Only if their type is () though.


i can see either statements or expressions. are there any kind like " expression statements" ? are these different from expressions themselves?
Function Definitions are also Statements

As I said before, this is one of five kinds of statements. This one is defined as one of two things:

  • ExpressionWithoutBlock, with mandatory semicolon;
  • ExpressionWithBlock, with optional semicolon.

These two kinds of expressions are further composed of more different cases, but the gist is simple, as @steffahn already said: expression which ends with a } is also a statement; other expressions are converted to statements with semicolon. In both cases this would be expression statements.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.