na severu a Hradec Králové je na sever od Pardubic • Středověcí hradečáci si utírali zadek mechem gardners.cz Montekové versus Kapuleti, Američani versus Rusáci, Microsoft versus Apple Hradec Králové versus Pardubice
• Idiomatický Rust je stejně rychlý jako idiomatické C++ • Syntaxe je podobná C / C++ • Paměťově bezpečný (nezná NULL, neplatné ukazatele, …) • Option, který nabývá hodnoty Some nebo None
+ volitelný „reference counting“ (Rc, Arc, …) • Upřednostňuje „stack allocation“ a nemá implicitní „boxing“ • Použití referencí (&) kontroluje „borrow checker“ při překladu • &T, &mut T, T • Jedna měnitelná reference nebo několik neměnitelných
mu „traity“ (inspirace u Haskellu) • Odvozuje typy kde se dá • Funkce mohou mít generické parametry vyžadující implementaci „traitu“ • Může proběhnout typová kontrola při překladu (C++20) • Generika - monomorfizace - separátní kopie kódu pro jednotlivé instance • Lépe se optimalizuje, prodlužuje čas kompilace a binárka je větší
Edition Guide - https://doc.rust-lang.org/edition-guide/ • The Rust Standard Library - https://doc.rust-lang.org/std/ • Crates docs - https://docs.rs • The Dark Arts of Advanced and Unsafe Rust Programming • https://doc.rust-lang.org/nomicon/
`x` !!--> src/main.rs:4:5 | 2 | let x = 5; | - | | | first assignment to `x` | help: make this binding mutable: `mut x` 3 | println!("The value of x is: {}", x); 4 | x = 6; | ^^^^^ cannot assign twice to immutable variable error: aborting due to previous error
fn main() { !// Sized !=> stack let a = 10; !// Sized & Copy !=> stack, kopie let b = a; !// Obe promenne jsou stale k dispozici println!("a: {}, b: {}", a, b); }
src/main.rs:4!:30 | 2 | let a = String!::from("foo"); | - move occurs because `a` has type `std::string::String`, which does not implement the `Copy` trait 3 | let b = a; | - value moved here 4 | println!("a: {}, b: {}", a, b); | ^ value borrowed here after move
!-> usize { s.len() } fn main() { let a = String!::from("foo"); !// Predame pomoci reference let l = len(&a); !// 'a' je tu porad s nami println!("Len of '{}' is {}", a, l); }
at a time !!--> src/main.rs:5:14 | 4 | let r1 = &mut s; | ------ first mutable borrow occurs here 5 | let r2 = &mut s; | ^^^^^^ second mutable borrow occurs here 6 | 7 | println!("{}, {}", r1, r2); | -- first borrow later used here
fn dangle() !-> &String { | ^ help: consider giving it a | 'static lifetime: `&'static` | = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
also borrowed as immutable !!--> src/main.rs:9:5 | 8 | let first = first_word(&s); | -- immutable borrow occurs here 9 | s.clear(); | ^^^^^^^^^ mutable borrow occurs here 10 | println!("First word: {:?}", first); | ----- immutable borrow later used here
100, 65]; println!("The largest number is {:?}", largest(&number_list)); let char_list = vec!['y', 'm', 'a', 'q']; println!("The largest char is {:?}", largest(&char_list)); }
impl Summarize for Person {} fn print_summary(x: &impl Summarize) { println!("{}", x.summary()); } fn main() { let person = Person {}; print_summary(&person); }
| 11 | l = longer(&a, &b); | ^^ borrowed value does not live | long enough 12 | } | - `b` dropped here while still borrowed 13 | 14 | println!("Longer: {}", l); | - borrow later used here