Learning Rust
gitbook
gitbook
  • Introduction
  • Basics
    • Why Rust
    • Installation
    • Hello World
    • Cargo,crates and basic project structure
    • Comments and documenting the code
    • Variable bindings, constants and statics
    • Functions
    • Primitive data types
    • Operators
    • Control flows
  • Beyond The Basics
    • Vectors
    • Structs
    • Enums
    • Generics
    • Impls and traits
  • The Tough Part
    • Ownership
    • Borrowing
    • Lifetimes
  • Lets Get It Started
    • Code organization
    • Functions
    • Modules
    • Crates
    • Workspaces
    • use
    • std, primitives and preludes
Powered by GitBook
On this page
  1. Lets Get It Started

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())
}
PreviousCode organizationNextModules

Last updated 7 years ago

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

attribute