SlideShare a Scribd company logo
1 of 16
Download to read offline
Rustの「所有権」について
Rustとは?
https://www.rust-lang.org/ja-JP/
型推論があってパターンマッチがあって並列性に優れていてジェネリクスがありつつ抽
象化に対して実行時のコストを必要としない(でも動的なディスパッチもできる)しコンパイ
ル後にはバイナリになるという
なんか最強っぽいプログラミング言語
RustにはGCが存在しません
そのためメモリ管理はコード上で行います、しかしC言語のように開発者が malloc - fee
関数で自力で管理する必要もありません、基本的には「所有権」
という概念によってメモリ管理が実装されています。
所有権はコンパイル時に管理されるためGCのように実行時にコストを必要とすることも
ありません。
所有権(ownership)
Rustの変数は実データへのアドレスの他、データに対する所有権を持ちます。
変数定義時にデータに対する参照アドレスと所有権を得ます。
★ イメージ
memory
User:
“takashi”
変数 ‘x’
参照
所有権
メモリに確保されたデータは所有権を持つ変数がスコープから外れた時点で開放されま
す
所有権の移動(move)
ある変数に束縛されたデータを他の変数に束縛することで所有権が移動します。
データへの所有権を持たない変数を使用するとコンパイルエラーとなります。
★ イメージ
memory
User:
“takashi”
変数 ‘x’
参照
所有権
変数 ‘y’
所有権の移動により変数’x’は
User:”takashi”への所有権をもたな
い
所有権を変数’y’が持つため、11行
目のタイミングでメモリから削除され
る
所有権の移動
借用(Borrowing)
他の変数の束縛時に参照を渡すことで所有権を借用させることができます、借用の場
合は所有権の移動が発生せず複数の変数からデータへのアクセスを許容することがで
きます。
★ イメージ
memory
User:
“takashi”
変数 ‘x’
参照
所有権
変数 ‘y’
所有権は変数’x’が持つため、15行
目時点までメモリから削除されない
し変数 ‘x’ を使用することも可能
借用
所有権を借用しているためUser:
“takashi” へのアクセスが許される
ちなみに自分より外のスコープにいる変数への借用はコンパイルエラーとなります
ライフタイム(おまけ)
参照は関数の引数の値としても使えます。
ただし2つ以上の参照型の引数を持つシグネチャはコンパイルエラーを引き起こします。
関数を含んだ借用の受け渡しは複雑化するため、コンパイラはライフタイムを明記して
いないシグネチャを受け入れません
ライフタイムを明記してあげましょう。
(シングルクォート + 任意の文字列がライフタイムの記法です、基本的にはaやbなど意
味を持たない文字列が使われることが多いようです?)
ライフタイムはその名の通り参照の持つ生存期間です、同じライフタイムを持つ変数は
同じあるひとつの所有権を持つ変数からの参照でなければいけません
つまり、上記シグネチャでは変数’l’を返した場合、ライフタイム不一致のためコンパイル
エラーとなります
ライフタイムの概念は所有権の中でも一番複雑なものなので日本語のドキュメントを読
むことをおすすめします。
(LTでは話しきれない & 砂川の理解も追いついていない)🍊
https://doc.rust-jp.rs/the-rust-programming-language-ja/1.9/book/lifetimes.html
まとめ
- 所有権はGCを使わず、開発者がメモリ管理をすることもなく効率的なメモリ空間の
管理を提供してくれる素敵な概念
- 所有権を持たない変数の使用はコンパイルエラーとなるためメモリ安全(開放され
たメモリにアクセスしない)
- 借用機能を利用することで所有権を移動させず自分より狭い(もしくは同じ)スコープ
対する参照の受け渡しができる
- ライフタイムを明記することで関数やstructで参照型が使える
- Rustすばらしいかもしれない
資料
- 公式
https://www.rust-lang.org/ja-JP/
- 日本語資料
https://doc.rust-jp.rs/the-rust-programming-language-ja/1.9/book/README.html
- The Book 2nd日本語PDF
https://y-yu.github.io/trpl-2nd-pdf/book.pdf
ご清聴ありがとうございました 🙇

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Rustの所有権について