CodeBlanks Logo Icon CodeBlanks

Summary - Rust basics

Combine fn main, println! and comments to write your first program.

Rust basics summary

In this group you learned Rust's goal (safety + speed), project management with Cargo (cargo new, run, build --release, test), the fn main entry point, the println!/print!/eprintln! macros, the {}/{:?}/{:.2} formats and the three kinds of comments.

fn main() {
    let dil = "Rust";
    let yil = 2015;
    println!("{dil} {yil} yılında 1.0 oldu");
}

The compiler is your friend

The Rust compiler's error messages are detailed: they point at the problem line, explain why, and often suggest a fix. Commands such as rustc --explain E0382 give a long explanation of each error code. cargo clippy provides extra suggestions and cargo fmt standard formatting.

Practice Introduction line

Declare let dil = "Rust"; and let yil = 2015;. Print println!("{dil} {yil} yılında 1.0 oldu"); and add an explanatory // comment before it.

Starter code

main.rs
fn main() {
    // buraya yaz
}

Sample solution

main.rs
fn main() {
    let dil = "Rust";
    let yil = 2015;
    // 1.0 sürümünün yılını yazdır
    println!("{dil} {yil} yılında 1.0 oldu");
}

Expected output

Rust 2015 yılında 1.0 oldu

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which doesn't compile?

  • println!("{} {}", 1); Correct answer
  • println!("{}", 1);
  • println!("{:?}", (1, 2));
  • print!("a");

Explanation: There are two placeholders but only one argument; println! catches this at compile time.

2 Format

Question
cargo ____   # formats the code to the standard style
Answer
cargo fmt   # formats the code to the standard style

Explanation: cargo fmt formats the code to the standard style with rustfmt.

3 Tool and role

Term Meaning
cargo fmt Formatting
cargo clippy Suggestions (lint)
rustc --explain Error explanation
cargo doc Documentation

All lessons in Beginner