rust for c++ programmers · • the same programming style won’t work to both languages: you have...

33
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

Upload: others

Post on 24-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

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;

Thank you.

33 Open Source Group – Samsung Research America (Silicon Valley) © 2014 Samsung Electronics Co.