Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The sweet flavour of Rust

The sweet flavour of Rust

Every chef now and then uses pinch of his secret spices to make a dish somewhat special and exquisite. This talk will provide introduction into nature of Rust sweetness and will answer to most important questions: Why and When does it make our dishes better? We will iterate over small code snippets and take a look How strong typing and functional thinking can be applied to low level system programming.

Avatar for Leonids Maslovs

Leonids Maslovs

March 03, 2015

Other Decks in Programming

Transcript

  1. - Unix, time-shared OS > ASM, C - Desktop as

    commodity, applications > C/C++, Java - Web > PHP, Python, Java, .Net - Smartphone > Java, Object-C, Swift - Mobile (battery), Internet of Things > C/C++, Go, ??? Mozilla strikes back 10 / 42
  2. Safe - Memory safety, no wild pointers - Typestate system,

    no null pointers - Mutability control, immutable by default - Side-effect control, pure by default 12 / 42
  3. Worldly - GC, green threads, stack unwinding - Minimal, yet

    detachable runtime - C bindings / FFI - Cross compile 13 / 42
  4. Typed - Not "everything is an object" - Multi-paradigm: Structs,

    Traits - Trait-based generics, Type inference, Pattern matching 16 / 42
  5. i n t m a i n ( v o

    i d ) { c h a r * s = " h e l l o w o r l d " ; * s = ' H ' ; } 19 / 42
  6. $ m a k e h e l l o

    & & . / h e l l o c c h e l l o . c - o h e l l o [ 1 ] 6 0 1 5 s e g m e n t a t i o n f a u l t . / h e l l o 20 / 42
  7. f n m a i n ( ) { l

    e t s = & " h e l l o w o r l d " ; * s = " H " ; } 21 / 42
  8. $ r u s t c h e l l

    o . r s h e l l o . r s : 3 : 5 : 3 : 1 3 e r r o r : c a n n o t a s s i g n t o i m m u t a b l e b o r r o w e d c o n t e n t ' * s ' h e l l o . r s : 3 * s = " H " ; ^ ~ ~ ~ ~ ~ ~ ~ e r r o r : a b o r t i n g d u e t o p r e v i o u s e r r o r 22 / 42
  9. # i n c l u d e < i

    o s t r e a m > # i n c l u d e < v e c t o r > # i n c l u d e < s t r i n g > i n t m a i n ( ) { s t d : : v e c t o r < s t d : : s t r i n g > v ; v . p u s h _ b a c k ( " H e l l o " ) ; s t d : : s t r i n g & x = v [ 0 ] ; v . p u s h _ b a c k ( " w o r l d " ) ; s t d : : c o u t < < x ; } 23 / 42
  10. $ m a k e h e l l o

    & & . / h e l l o g + + h e l l o . c p p - o h e l l o [ 1 ] 8 9 2 3 s e g m e n t a t i o n f a u l t . / h e l l o 24 / 42
  11. f n m a i n ( ) { l

    e t m u t v = v e c ! [ ] ; v . p u s h ( " H e l l o " ) ; l e t w = & v [ 0 ] ; v . p u s h ( " w o r l d " ) ; p r i n t l n ! ( " { } w o r l d " , w ) ; } 25 / 42
  12. $ r u s t c v - b o

    r r o w . r s v - b o r r o w . r s : 8 : 5 : 8 : 6 e r r o r : c a n n o t b o r r o w ' v ' a s m u t a b l e b e c a u s e i t i s a l s o b o r r o w e d a s i m m u t a b l e v - b o r r o w . r s : 8 v . p u s h ( " w o r l d " ) ; ^ v - b o r r o w . r s : 6 : 1 4 : 6 : 1 5 n o t e : p r e v i o u s b o r r o w o f ' v ' o c c u r s h e r e ; t h e i m m u t a b l e b o r r o w p r e v e n t s s u b s e q u e n t m o v e s o r m u t a b l e b o r r o w s o f ' v ' u n t i l t h e b o r r o w e n d s v - b o r r o w . r s : 6 l e t w = & v [ 0 ] ; ^ v - b o r r o w . r s : 1 1 : 2 : 1 1 : 2 n o t e : p r e v i o u s b o r r o w e n d s h e r e v - b o r r o w . r s : 1 f n m a i n ( ) { . . . v - b o r r o w . r s : 1 1 } ^ e r r o r : a b o r t i n g d u e t o p r e v i o u s e r r o r 26 / 42
  13. u s e s t d : : t h

    r e a d ; f n m a i n ( ) { f o r _ i n 0 . . 5 { t h r e a d : : s c o p e d ( | | { p r i n t l n ! ( " H e l l o , w o r l d ! " ) ; } ) ; } ; } 28 / 42
  14. u s e s t d : : t h

    r e a d ; u s e s t d : : s y n c : : m p s c ; u s e s t d : : s y n c : : m p s c : : * ; f n m a i n ( ) { l e t ( t x , r x ) : ( S e n d e r < i 3 2 > , R e c e i v e r < i 3 2 > ) = m p s c : : c h a n n e l ( ) ; l e t t h r e a d _ t x = t x . c l o n e ( ) ; t h r e a d : : s p a w n ( m o v e | | { t h r e a d _ t x . s e n d ( 1 0 ) . o k ( ) . e x p e c t ( " U n a b l e t o s e n d m e s s a g e " ) ; } ) ; p r i n t l n ! ( " { : ? } " , r x . r e c v ( ) ) ; } 29 / 42
  15. f n m a i n ( ) { l

    e t m u t n u m b e r s = v e c ! [ 1 , 2 , 3 ] ; f o r _ i n 0 . . 3 { s t d : : t h r e a d : : s c o p e d ( m o v e | | { f o r j i n 0 . . 3 { n u m b e r s [ j ] + = 1 } } ) ; } } 31 / 42
  16. $ r u s t c b o r r

    o w . r s b o r r o w . r s : 6 : 2 9 : 6 : 3 6 e r r o r : c a p t u r e o f m o v e d v a l u e : ' n u m b e r s ' b o r r o w . r s : 6 f o r j i n 0 . . 3 { n u m b e r s [ j ] + = 1 } ^ ~ ~ ~ ~ ~ ~ b o r r o w . r s : 5 : 3 4 : 7 : 1 0 n o t e : ' n u m b e r s ' m o v e d i n t o c l o s u r e e n v i r o n m e n t h e r e b e c a u s e i t h a s t y p e ' [ c l o s u r e ( ( ) ) ] ' , w h i c h i s n o n - c o p y a b l e b o r r o w . r s : 5 s t d : : t h r e a d : : s c o p e d ( m o v e | | { b o r r o w . r s : 6 f o r j i n 0 . . 3 { n u m b e r s [ j ] + = 1 } b o r r o w . r s : 7 } ) ; b o r r o w . r s : 5 : 3 4 : 7 : 1 0 h e l p : p e r h a p s y o u m e a n t t o u s e ' c l o n e ( ) ' ? b o r r o w . r s : 5 s t d : : t h r e a d : : s c o p e d ( m o v e | | { b o r r o w . r s : 6 f o r j i n 0 . . 3 { n u m b e r s [ j ] + = 1 } b o r r o w . r s : 7 } ) ; 32 / 42
  17. u s e s t d : : t h

    r e a d ; u s e s t d : : s y n c : : { A r c , M u t e x } ; f n m a i n ( ) { l e t d a t a = A r c : : n e w ( M u t e x : : n e w ( v e c ! [ 0 ; 5 ] ) ) ; ( 0 . . 5 ) . m a p ( | i | { l e t m u t e x = d a t a . c l o n e ( ) ; t h r e a d : : s c o p e d ( m o v e | | { l e t m u t v e c = m u t e x . l o c k ( ) . u n w r a p ( ) ; v e c [ i ] + = 1 ; } ) } ) . c o l l e c t : : < V e c < _ > > ( ) ; p r i n t l n ! ( " { : ? } " , * d a t a . l o c k ( ) . u n w r a p ( ) ) ; } 33 / 42
  18. f n m a i n ( ) { l

    e t x = 5 ; m a t c h x { 1 | 2 = > p r i n t l n ! ( " o n e o r t w o " ) , 3 = > p r i n t l n ! ( " t h r e e " ) , e @ 1 . . . 5 = > p r i n t l n ! ( " o n e t h r o u g h f i v e , { } " , e ) , e i f e < 1 0 = > p r i n t l n ! ( " l e s s t h a n 1 0 , { } " , e ) , _ = > p r i n t l n ! ( " a n y t h i n g " ) , } } 35 / 42
  19. u s e s t d : : i t

    e r : : i t e r a t e ; f n m a i n ( ) { l e t f i b o n a c c i = i t e r a t e ( ( 0 u 3 2 , 1 u 3 2 ) , | e | { ( e . 1 , e . 0 + e . 1 ) } ) . s k i p ( 1 ) . m a p ( | e | e . 1 ) ; l e t n u m b e r s : V e c < _ > = f i b o n a c c i . t a k e ( 1 0 ) . c o l l e c t ( ) ; p r i n t l n ! ( " { : ? } " , n u m b e r s ) ; } 36 / 42
  20. e x t e r n m o d a

    c t i v e _ s u p p o r t ; u s e a c t i v e _ s u p p o r t : : P e r i o d ; f n m a i n ( ) { p r i n t l n ! ( " { : ? } " , 2 . d a y s ( ) . f r o m _ n o w ( ) ) ; p r i n t l n ! ( " { : ? } " , 2 . w e e k s ( ) . f r o m _ n o w ( ) ) ; p r i n t l n ! ( " { : ? } " , 2 . m o n t h s ( ) . f r o m _ n o w ( ) ) ; p r i n t l n ! ( " { : ? } " , 2 . y e a r s ( ) . f r o m _ n o w ( ) ) ; } p u b t r a i t P e r i o d { f n d a y s ( & s e l f ) - > T i m e C h a n g e ; f n w e e k s ( & s e l f ) - > T i m e C h a n g e ; . . . } i m p l P e r i o d f o r u 3 2 { f n d a y s ( & s e l f ) - > T i m e C h a n g e { T i m e C h a n g e : : n e w ( ) . d a y s ( * s e l f a s f 3 2 ) } . . . } 37 / 42
  21. Introductions - 30 min introduction into Rust http://doc.rust-lang.org/nightly/intro.html - Guide,

    Introduction Book http://doc.rust-lang.org/book/ - Rust by Example http://rustbyexample.com/ - Rust for CXX programmers https://github.com/rust-lang/rust/wiki/Rust-for-CXX-programmers http://www.quora.com/What-do-C-C++-systems-programmers-think-of-Rust 39 / 42
  22. Discussions / News - User forum http://users.rust-lang.org/ - Reddit /r/rust

    http://www.reddit.com/r/rust/ - #rust on irc.mozilla.org https://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust - This Week in Rust http://this-week-in-rust.org/ 40 / 42