Functions

Functions are the first line of organization in any program.

fn main() {
  greet(); //do one thing
  ask_location(); //do another thing
}

fn greet() {
  println!("Hello!");
}

fn ask_location() {
  println!("Where are you from?");
}

We can add unit tests in the same file.

fn main() {
    greet();
}

fn greet() -> String {
    "Hello, world!".to_string()
}

#[test] // test attribute indicates, this is a test function
fn test_greet() {
    assert_eq!("Hello, world!", greet())
}

💭 An attribute is a general, free-form metadatum that is interpreted according to name, convention, and language and compiler version.

Last updated