![Page 1: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/1.jpg)
1 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Adenilson Cavalcanti BSc MSc
WebKit & Blink committer W3C CSS WG member
Rust for C++ programmers
![Page 2: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/2.jpg)
2 Open Source Group – Samsung Research America (Silicon Valley) © 2014 Samsung Electronics Co.
Summary
Introduction
The case for Rust
Operators x traits
Templates x generics
Macros x attributes
Threads x tasks
Heap x boxes
![Page 3: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/3.jpg)
3 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Introduction
The problem: • There are many C++ programmers • But few Rust programmers
The solution: Why not explain Rust comparing it with modern C++11?
![Page 4: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/4.jpg)
4 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ x Rust
![Page 5: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/5.jpg)
5 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Why Rust?
• It aims to solve some gruesome issues in C/C++.
• Be more expressive for hard tasks. • Safety X performance: have both!
The case for Rust
![Page 6: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/6.jpg)
6 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ example: anything wrong here?
http://doc.rust-lang.org/nightly/intro.html#ownership
![Page 7: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/7.jpg)
7 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Run on valgrind: warnings
![Page 8: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/8.jpg)
8 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Using gdb to understand what is happening…
![Page 9: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/9.jpg)
9 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Address: 0x100103af0
Address
![Page 10: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/10.jpg)
10 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Change! Now: 0x100103b10
Address
![Page 11: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/11.jpg)
11 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Ref points to old address!
Compiler warnings won’t catch it:
![Page 12: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/12.jpg)
12 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Rust to the rescue!
![Page 13: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/13.jpg)
13 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Rust stops bugs from happening.
![Page 14: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/14.jpg)
14 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Introduction
• A C++ example is given • A similar Rust program is presented
How it is done
![Page 15: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/15.jpg)
15 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Introduction
• Get used to Rust syntax • Compare it with a mainstream language
Objective
![Page 16: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/16.jpg)
16 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ operators
• Allows to overload operators (e.g. +, -, (), [], etc)
• Improves code readability and reuse
Use
![Page 17: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/17.jpg)
17 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ operators
![Page 18: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/18.jpg)
18 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Rust traits
![Page 19: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/19.jpg)
19 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ templates
• Parametric types at compile type
• Improves performance and code reuse
Use
![Page 20: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/20.jpg)
20 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ templates
![Page 21: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/21.jpg)
21 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Rust generics
//http://doc.rust-lang.org/src/core/home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libcore/mem.rs.html#270-284 http://en.cppreference.com/w/cpp/utility/move
![Page 22: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/22.jpg)
22 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ macros
• Conditional compilation, performance code, etc
• When possible, use inline functions instead.
Use
![Page 23: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/23.jpg)
23 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ macros
![Page 24: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/24.jpg)
24 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Rust attributes
![Page 25: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/25.jpg)
25 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ threads
• Parallelism, performance.
• Can get messy pretty fast.
Use
![Page 26: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/26.jpg)
26 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
![Page 27: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/27.jpg)
27 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Rust tasks
![Page 28: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/28.jpg)
28 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ heap
• Memory allocation of objects (i.e. stack has a fixed size).
• Be careful with leaks (smart pointers are your friend).
Use
![Page 29: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/29.jpg)
29 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
C++ heap use
![Page 30: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/30.jpg)
30 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Rust boxes
![Page 31: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/31.jpg)
31 © 2014 Samsung Electronics Co. Open Source Group – Samsung Research America (Silicon Valley)
Conclusions • Rust has equivalent features to modern C++; • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code; • For more complex tasks (i.e. multitask), Rust seems to be more expressive; • Most importantly: Rust avoids common traps in C++.
![Page 32: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/32.jpg)
32 Open Source Group – Samsung Research America (Silicon Valley) © 2014 Samsung Electronics Co.
Questions?
![Page 33: Rust for C++ programmers · • The same programming style won’t work to both languages: you have to code in the right style; • For smaller tasks, C++ may have more compact code;](https://reader033.vdocuments.us/reader033/viewer/2022042301/5ecccf7cc221095fc21e2ba6/html5/thumbnails/33.jpg)
Thank you.
33 Open Source Group – Samsung Research America (Silicon Valley) © 2014 Samsung Electronics Co.