Rust functions: Difference between revisions

From wikinotes
(Created page with "= Function Signatures = <blockquote> <syntaxhighlight lang="rust"> fn main() { println!("hi"); } </syntaxhighlight> </blockquote><!-- Function Signatures -->")
 
No edit summary
Line 7: Line 7:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Function Signatures -->
</blockquote><!-- Function Signatures -->
= Expressions vs Statements =
<blockquote>
* statements include actions without a return value (ends in ;)
* expressions include actions with a return value (no ;)
statement
<syntaxhighlight lang="rust">
{
    let y = 1;
    y += 1;
} // no return val
</syntaxhighlight>
expression
<syntaxhighlight lang="rust">
let x = {
    let y = 1;
    y += 1      // <-- no semicolon
} // returns 2
</syntaxhighlight>
</blockquote><!-- Expressions vs Statements -->

Revision as of 00:47, 7 February 2023

Function Signatures

fn main() {
    println!("hi");
}

Expressions vs Statements

  • statements include actions without a return value (ends in ;)
  • expressions include actions with a return value (no ;)

statement

{
    let y = 1;
    y += 1;
} // no return val

expression

let x = {
    let y = 1;
    y += 1       // <-- no semicolon
} // returns 2