Muuttujat ja tyypit


  • Kurssimateriaali

doc.fablab.rip

  • Liittykää Telegram:

lyli.fi/rust


let

use std::io;

fn main() {
    println!("Guess the number!");

    println!("Please input your guess.");

    let mut guess = String::new();

    io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");

    println!("You guessed: {}", guess);
}

use std::io;

fn main() {
    // ...

    let mut guess = String::new();

    io::stdin();

    // ...
}
from std import io

def main():
    # ...

    guess = String()

    io.stdin()

    # ...

use

// use std::io;

fn main() {
    // ...

    let mut guess = String::new();

    std::io::stdin();

    // ...
}

mut

#![allow(unused)]
fn main() {
let guess = String::new();

std::io::stdin()
    .read_line(&mut guess)
    .expect("Failed to read line");
}

#![allow(unused)]
fn main() {
error[E0596]: cannot borrow `guess` as mutable, as it is not declared as mutable
 --> a.rs:5:20
  |
2 |     let guess = String::new();
  |         ----- help: consider changing this to be mutable: `mut guess`
...
5 |         .read_line(&mut guess)
  |                    ^^^^^^^^^^ cannot borrow as mutable

error: aborting due to previous error
}

fn

#![allow(unused)]
fn main() {
fn hello(name: &str) {
    println!("Hello, {}", name);
}
}

#![allow(unused)]
fn main() {
fn bad_max(x: i32, y: i32) -> i32 {
    let maximum;
    if x > y {
        maximum = x;
    } else {
        maximum = y;
    }
    return maximum;
}

fn better_max(x: i32, y: i32) -> i32 {
    let maximum = if x > y {
        x
    } else {
        y
    };
    maximum
}
}

if-lauseke

#![allow(unused)]
fn main() {
fn best_max(x: i32, y: i32) -> i32 {
    if x > y { x } else { y }
}
}

Scope

#![allow(unused)]
fn main() {
let x = 5;
let y = 3;

if x > y {
    let diff = x - y;
}
println!("{}", diff);
}

Scope

#![allow(unused)]
fn main() {
error[E0425]: cannot find value `diff` in this scope
 --> /tmp/mdbook-zzWDbB/week2/slides.md:164:16
  |
9 | println!("{}", diff);
  |                ^^^^ not found in this scope

error: aborting due to previous error
}

Scope ratkaisu?

#![allow(unused)]
fn main() {
let x = 5;
let y = 3;

let mut diff;
if x > y {
    diff = x - y;
}
println!("{}", diff);
}

Scope ratkaisu?

#![allow(unused)]
fn main() {
error[E0381]: borrow of possibly-uninitialized variable: `diff`
  --> /tmp/mdbook-HR2wgB/week2/slides.md:198:16
   |
10 | println!("{}", diff);
   |                ^^^^ use of possibly-uninitialized `diff`
   |

error: aborting due to previous error
}

let x = 5;
{               // näkyvissä: x
    let y = 3;
    {           // näkyvissä: x, y
        if x > y {
            let diff = x - y;
            {   // näkyvissä: x, y, diff
                println!("{}", diff);
            }
        }       // diff pudotetaa
    }
}               // y pudotetaan

Omistajuus

#![allow(unused)]
fn main() {
let x = String::from("hello");
let y = x;
println!("{}", x);
}

#![allow(unused)]
fn main() {
error[E0382]: borrow of moved value: `x`
 --> /tmp/mdbook-7pr3zz/week2/slides.md:208:16
  |
3 | let x = String::from("hello");
  |     - move occurs because `x` has type `String`,
  |       which does not implement the `Copy` trait
4 | let y = x;
  |         - value moved here
5 | println!("{}", x);
  |                ^ value borrowed here after move

error: aborting due to previous error
}

Lainaus

#![allow(unused)]
fn main() {
let mut s = String::from("hello");
// push_str lainaa `s` mutablena
s.push_str(", world!");
println!("{}", s);
}

Rust pähkinänkuoressa

Rust in a nutshell


Otarustlings

cargo install otarustlings # might take a while
cd Desktop
otarustlings init

bongo