30
OOP in Rust Rust Created by KENZ / @KENZ_gelsoft

OOP in Rust

Embed Size (px)

DESCRIPTION

Rust 言語のオブジェクト指向についてです。 「Rust Samurai 参」で発表した内容です。 http://atnd.org/events/46854 背景が見づらかったのでReveal.jsのテーマを変えてアップロードしなおしました。

Citation preview

Page 1: OOP in Rust

OOP in RustRust s©Õ·¦®Ç®¹

Created by KENZ / @KENZ_gelsoft

Page 2: OOP in Rust

Rust t{It supports a mixture of imperative procedural,

concurrent actor, object-oriented and purefunctional styles. Rust also supports generic

programming and metaprogramming, in bothstatic and dynamic styles.

Page 3: OOP in Rust

Rust t{Rust { C++ vut�FwÝêÀÐè¿£ßz{§

��Y�Öì¯èÞò¯ Ê¡®¾�áÆêÖì¯èÞò¯©Õ·¦®Ç®¹Öì¯èÞò¯�D�Öì¯èÞò¯·¦Ìé®Öì¯èÞò¯à¾Öì¯èÞò¯

�x{©Õ·¦®Ç®¹wpPr÷��

Page 4: OOP in Rust

©Õ·¦®Ç®¹t{("http://ja.wikipedia.org/wiki/©Õ·¦®Ç®¹Öì¯èÞò¯" ��)

©Õ·¦®Ç®¹Öì¯èÞò¯t{|©Õ·¦®Çt�|��AÿzóÉs¼ÔÇ¥¦¡�Ö\ag��zsN�|´m0w�AzAÿ�§Î�ËJckÖì¯èÞò¯[]z_t�PR}

ªÖºêB (��ðPzÉ!tÆ�¾É!)£ò×é¾ò¸ (.¦) -- ®è¸Ø�¸z{§ÜéáÔ¢¹ß (*A[|*ö[) -- �ÔYz{§¿£ÉÞ®ϣòÆ¢ò¯ (�0�>) -- �0�Ô{§

(�)

_��zAÿzRm|©Õ·¦®Ç®¹zæT[sÓIDvz{°ªÖºêB±zAÿl]sN�}

(�)

Page 5: OOP in Rust

ªÖºêBt{("http://ja.wikipedia.org/wiki/ªÖºêB" ��)

ªÖºêB¤ªÖºêW|encapsulation¥t{|©Õ·¦®Ç®¹�Ö\e��òz´p}©Õ·¦®ÇÆózÆ�¾�É!ck�(Æ�¾É!)|©Õ·¦®Çz��ðP�É!ck�|©Õ·¦®ÇzöUz��É!ck�e�_t�PR}Æ�¾É!të«Pa��ePX|Æ�¾É!{ªÖºêBzï9ºz1pweZf|�´z�zs{vP}

Page 6: OOP in Rust

�a{ö�N�X|P{©Õ·¦®Çt{DWàº�·���(à¼ÂÈ�àòÏ�D���ce)tv���[�W��]u�Ìcr[����ce�XÍcP_t��v[rPP

¼ÔÇ¥¦¡zAÿ�©Õ·¦®ÇzÔ���gsöxe�zX©Õ·¦®Ç®¹Öì¯èÞò¯tPor�P

Page 7: OOP in Rust

©Õ·¦®Ç®¹{v�s4cPz�

©Õ·¦®Ç��R�{©Õ·¦®Çz_t���v[rPP©Õ·¦®Çti���R²�È�*�z�Xö�sY�¼ÔÇ¥¦¡�PzÆB¤_�X»�¥�Ôw|¼ÔÇ¥¦¡óÉz>qJ[�û�k

Page 8: OOP in Rust

Rust s{uR�[z�uR�orà¼ÂÈ��ae�z�âå�µ�a�w;cr impl ��ae�Objective-C z @implementation tWC++ s .h s®è¸�{cr .cpp sö�e��kPv£à�·

Page 9: OOP in Rust

��a�t{�structtuple structenum

Page 10: OOP in Rust

structC zÖ�9�kPv�p

struct Point { x: int, y: int}let p = Point {x: 10, y: 11};let px: int = p.x;

Page 11: OOP in Rust

tuple structÔ¢�êÈØXpPrPvPÖ�9|

match äsz�¯����cg�struct Point(int, int);let p = Point(10, 11);let px: int = match p { Point(x, _) => x };

Page 12: OOP in Rust

enumC zÊ�wßP÷d

enum Animal { Dog, Cat}let mut a: Animal = Dog;a = Cat;

Page 13: OOP in Rust

enum - 2a�w|©�w;cr struct �

tuple struct z�RwÔ¢�êÈ�Ór�

enum Animal { Dog (~str, f64), Cat { name: ~str, weight: f64 }}let mut a: Animal = Dog(~"Cocoa", 37.2);a = Cat{ name: ~"Spotty", weight: 2.7 };

Page 14: OOP in Rust

impl�w;crà¼ÂÈ�ö�e�w{ impl ��R}º:use std::num::{sqrt,pow};struct Point { x: int, y: int}impl Point { fn zero() -> Point { Point(0, 0) } fn distance(&self, rhs: &Point) -> f32 { let dx = self.x - rhs.x; let dy = self.y - rhs.y; sqrt( pow(dx as f32, 2f32) + pow(dy as f32, 2f32) ); }}

Page 15: OOP in Rust

impl - 2S´ÂDX§*vÂD self, python zà¼ÂÈ�kPv÷d

self {©$Ü£ò¾� ~self, &self, @self vus�OKfn distance(&self, rhs: &Point) -> f32 { ⋯ }

S´ÂDX self sv]�| static à¼ÂÈwv�

Page 16: OOP in Rust

trait´pz�wà¼ÂÈ��ae�[]{Wok}

d�N|�Dz�s�d£ò¾�Ô¦£¸�ö�e�w{�

trait ��Rtrait { Java z interface z�Rv�zå�µ�a�{�Dz trait �ö�sY�

Page 17: OOP in Rust

trait - 2trait z�{{Õz�Rv÷d|�[N�£ò¾�Ô¦£¸�{}

trait Shape { fn draw(&self, Surface); fn bounding_box(&self) -> BoundingBox;}

Page 18: OOP in Rust

trait zö�__s� impl ��R

impl TRAIT for TYPE { ... }

struct Circle { radius: f64, center: Point,}impl Shape for Circle { fn draw(&self, s: Surface) { do_draw_circle(s, *self); } fn bounding_box(&self) -> BoundingBox { let r = self.radius; BoundingBox{x: self.center.x - r, y: self.center.y - r, width: 2.0 * r, height: 2.0 * r} }}

Page 19: OOP in Rust

trait zö�å�µ�a�w�Dz trait �ö�sY�=#z�wßz trait w;e�ö��ðHe�_t�sY�_z�Rw|N��Xö�e� trait � Ô]s«ÌsY�tPR[õ� Adhoc Polyphormism tPRRust z trait { Haskell z Type Class zì¿� öý[*]k�zt­��ckX|ÍQ{��vPzs�or��³Tr[laP}

Page 20: OOP in Rust

.¦�z.¦{vP

To��zimplzö�.¦{vP

£ò¾�Ô¦£¸(trait)z.¦{N�

Page 21: OOP in Rust

Æ�¾É!pub, priv vuÆ�¾¡®º¸7ï¨{N�Æ�¾É!{N�X|á·ä�êz��d¼�¸Ô £ê¯s{e�r¡®º¸sY�

Page 22: OOP in Rust

Rust OOP z öU�ï�r{_�v��|öUuR�or�or�z�

trait ��o|w�Rо�òenum �s impl Æ match s6о�ò

Page 23: OOP in Rust

trait ��o|w�Rо�ò

Tutorial tW Manual tWs�W�r�cÏ]�d£ò¾�Ô¦£¸s�PkP�`twimplc�[�Cwö��¤&ckPÝ�{|traitzÆÔ¨êÇö���RN�P{|mix-in0wö��¤&trait Shape { fn area() -> f64; }trait Circle : Shape { fn radius() -> f64; }

trait Foo { fn default_impl() { /* Default Implementation */ }}

Page 24: OOP in Rust

trait ��o|w�Rо�ò - 2

Ø�¸ztrait(supertrait)zà¼ÂÈ���w{|

Ü£ò¾�w¬â¸Çe�W

let mycircle: Circle = @mycircle as @Circle;let nonsense = mycircle.radius() * mycircle.area();

Page 25: OOP in Rust

trait ��o|w�Rо�ò - 3

�Ðèà¾XØ�¸Çë£Ç�ö�crP�_t�ÚÜe�_tssY�

fn radius_times_area<T: Circle>(c: T) -> f64 { // ̀ ̀is both a Circle and a Shape c.radius() * c.area()}

c

Page 26: OOP in Rust

enum �s impl Æ matchs6о�ò

´�äcW©Õ·¦®Ç�äXvPÝ�{zv

enum Foo { A, B}impl Foo { fn do_task(&self) { match *self { A => do_a_thing(), B => do_b_thing() } }}

Page 27: OOP in Rust

Rust OOP �t�ªÖºêBN�|áQÉ!���P|®è¸Ø�¸WÖìǾ£ÖØ�¸WsPRt®è¸Ø�¸wßPX|®è¸�.¦{vP£ò¾�Ô¦£¸t£ò¾�Ô¦£¸.¦{N�COMwßP

Page 28: OOP in Rust

Rust OOP �t� - 2®è¸ å�µ�a�timpl£ò¾�Ô¦£¸ traittimpl.¦ ö{vP|

£ò¾�Ô¦£¸z.¦z�

Page 29: OOP in Rust

V�]Generics Programming

�w�#cvP¡ê³é¹ß�©Õ·¦®ÇzMe

�DÅòÖë�Ç·¦Ìé®�D|�ÅòÖë�Ç·¦Ìé®�N�ÅòÖë�Çà¾Öì¯èÞò¯{sYvPzs C++�z��z[�{VB�AaPuR��?ßðHa��[¹z�Rse…

Java�kPvtype erasure{cvP|C#z·¦Ì鮸wßP

Page 30: OOP in Rust

õ_-

´¾¦éƵwxRustRust ¹]�´(*1)zöJ(*2)GUIÃ�ê¬ÂÇ34[�PzÆÏ£¸s���d�vPWv|���]u

*1: 2013ñ²;Û�

*2: �cWck�öJwv�W�c�vPëØê