SlideShare a Scribd company logo
郭⾄至軒 [:kuoe0]
Mozilla
kuoe0.tw@gmail.com
Ownership
System in Rust
2016/07/12 @摩茲⼯工寮
4.8 Ownership[link]
Variable & Memory
A variable name is only a name.
It’s possible that a variable name can not access any
memory.
When a variable is declared, Rust allocates memory in
stack and heap (if need) for it.
When the owner of resources is destroyed, ALL
resources it owned would be released.
Variable & Memory
Stack
Heap
fn main() {
let v = vec![1, 2, 3];
}
dynamic memory
static memory
Variable & Memory
Stack
Heap
fn main() {
let v = vec![1, 2, 3];
}
dynamic memory
static memory
When the variable is destroyed…
fn main() {
let v = vec![1, 2, 3];
}
Variable & Memory
Stack
Heap
dynamic memory
static memory
All related resources will be destroyed, too!
Move By Default
Assignment operator is move semantics by default.
There is exactly one variable binding to any resource.
Avoid data racing to guarantee data consistency.
Move By Default
struct Point {
x: i32,
y: i32
}
fn main() {
let v1 = Point{ x: 10, y:
20};
let v2 = v1;
println!("{}", v1.x);
}
error: use of moved value: `v1.x` [--explain
E0382]
--> <anon>:9:20
8 |> let v2 = v1;
|> -- value moved here
9 |> println!("{}", v1.x);
|> ^^^^ value used here
after move
<std macros>:2:27: 2:58: note: in this
expansion of format_args!
<std macros>:3:1: 3:54: note: in this
expansion of print! (defined in <std macros>)
<anon>:9:5: 9:26: note: in this expansion of
println! (defined in <std macros>)
note: move occurs because `v1` has type
`Point`, which does not implement the `Copy`
trait
error: aborting due to previous error
compile
Move By Default
struct Point {
x: i32,
y: i32
}
fn main() {
let v1 = Point{ x: 10, y:
20};
let v2 = v1;
println!("{}", v1.x);
}
error: use of moved value: `v1.x` [--explain
E0382]
--> <anon>:9:20
8 |> let v2 = v1;
|> -- value moved here
9 |> println!("{}", v1.x);
|> ^^^^ value used here
after move
<std macros>:2:27: 2:58: note: in this
expansion of format_args!
<std macros>:3:1: 3:54: note: in this
expansion of print! (defined in <std macros>)
<anon>:9:5: 9:26: note: in this expansion of
println! (defined in <std macros>)
note: move occurs because `v1` has type
`Point`, which does not implement the `Copy`
trait
error: aborting due to previous error
compile
Use of moved value!
v1.x
Move By Default
stack
struct Point {
x: i32,
y: i32
}
fn main() {
let v1 = Point{ x: 10, y:
20};
let v2 = v1;
println!("{}", v1.x);
}
Point { x = 10, y = 20 }
names
v1
Move By Default
stack
struct Point {
x: i32,
y: i32
}
fn main() {
let v1 = Point{ x: 10, y:
20};
let v2 = v1;
println!("{}", v1.x);
}
Point { x = 10, y = 20 }
names
v1
v2
Copyable Type
The types which implement Copy trait can make
assignment operator be copy semantics.
Allow to use the variable which be copied.
All primitive types implement the Copy trait.
Copyable Type
fn main() {
let v1 = 10;
let v2 = v1;
println!("v1 = {}", v1);
println!("v2 = {}", v2);
}
v1 = 10
v2 = 10
Program ended.
run
Copyable Type
fn main() {
let v1 = 10;
let v2 = v1;
println!("v1 = {}", v1);
println!("v2 = {}", v2);
}
stacknames
v1 i32 { 10 }
Copyable Type
fn main() {
let v1 = 10;
let v2 = v1;
println!("v1 = {}", v1);
println!("v2 = {}", v2);
}
stacknames
v1 i32 { 10 }
v2 i32 { 10 }
Parameter Passing
Passing parameters is also move semantics by default
(no Copy trait).
Developers should return the ownership of parameters
by themselves.
Yes, you should return ten variables back if you pass
ten parameters into a function. 😜
Parameter Passing
struct Pt { x: i32, y: i32 }
fn dist(v: Pt) -> Pt {
println!("{}", v.x * v.x + v.y *
v.y);
v
}
fn main() {
let v = Pt{ x: 3, y: 4 };
let v = dist(v);
println!("{} {}", v.x, v.y);
}
struct Pt { x: i32, y: i32 }
fn dot(v1: Pt, v2: Pt) -> (Pt, Pt) {
println!("{}", v1.x * v2.x +
v1.y * v2.y);
(v1, v2)
}
fn main() {
let v1 = Pt{ x: 3, y: 4 };
let v2 = Pt{ x: 1, y: 2 };
let (v1, v2) = dot(v1, v2);
println!("{} {}", v1.x, v1.y);
println!("{} {}", v2.x, v2.y);
}
one parameter two parameters
Parameter Passing
struct Pt { x: i32 }
fn square(v: Pt) {
println!("{}", v.x * v.x);
}
fn main() {
let v = Pt{ x: 3 };
square(v);
println!("{}", v.x);
}
error: use of moved value: `v.x` [--explain
E0382]
--> <anon>:10:20
9 |> square(v);
|> - value moved here
10 |> println!("{}", v.x);
|> ^^^ value used here
after move
<std macros>:2:27: 2:58: note: in this
expansion of format_args!
<std macros>:3:1: 3:54: note: in this
expansion of print! (defined in <std macros>)
<anon>:10:5: 10:25: note: in this expansion
of println! (defined in <std macros>)
note: move occurs because `v` has type `Pt`,
which does not implement the `Copy` trait
error: aborting due to previous error
compile
Parameter Passing
struct Pt { x: i32 }
fn square(v: Pt) {
println!("{}", v.x * v.x);
}
fn main() {
let v = Pt{ x: 3 };
square(v);
println!("{}", v.x);
}
error: use of moved value: `v.x` [--explain
E0382]
--> <anon>:10:20
9 |> square(v);
|> - value moved here
10 |> println!("{}", v.x);
|> ^^^ value used here
after move
<std macros>:2:27: 2:58: note: in this
expansion of format_args!
<std macros>:3:1: 3:54: note: in this
expansion of print! (defined in <std macros>)
<anon>:10:5: 10:25: note: in this expansion
of println! (defined in <std macros>)
note: move occurs because `v` has type `Pt`,
which does not implement the `Copy` trait
error: aborting due to previous error
compile
v.x
Use of moved value!
4.9 Reference and Borrowing[link]
Syntax of Reference
fn main() {
let a = 1;
let b = &a; // &a is the reference to a
let mut c = 2;
let d = &mut c; // &mut c is the mutable
reference to c
}
Borrowing
Use the references to borrow the ownership.
The ownership will return to original owner when the
borrower is destroyed automatically.
References are immutable.
Allow multiple references to one variable.
A borrowed variable can be read but not written.
Only allow to borrow the variable with longer lifetime.
Borrowing
fn main() {
let orig = 0;
let b1 = &orig;
let b2 = &orig;
let b3 = &orig;
println!("b1 = {}", b1);
println!("b2 = {}", b2);
println!("b3 = {}", b3);
println!("orig = {}", orig);
}
b1 = 0
b2 = 0
b3 = 0
orig = 0
Program ended.
run
Borrowing
fn main() {
let mut x = 0;
{
let y = &x;
x += 1;
println!("{}", y);
}
println!("{}", x);
}
error: cannot assign to `x` because it is
borrowed [--explain E0506]
--> <anon>:5:9
4 |> let y = &x;
|> - borrow of `x` occurs
here
5 |> x += 1;
|> ^^^^^^ assignment to borrowed
`x` occurs here
error: aborting due to previous error
compile
Borrowing
fn main() {
let mut x = 0;
{
let y = &x;
x += 1;
println!("{}", y);
}
println!("{}", x);
}
error: cannot assign to `x` because it is
borrowed [--explain E0506]
--> <anon>:5:9
4 |> let y = &x;
|> - borrow of `x` occurs
here
5 |> x += 1;
|> ^^^^^^ assignment to borrowed
`x` occurs here
error: aborting due to previous error
compile
x += 1;
Cannot write the borrowed variable!
Borrowing
fn main() {
let y: &i32;
{
let x = 5;
y = &x;
}
println!("{}", y);
}
error: `x` does not live long enough
--> <anon>:5:14
5 |> y = &x;
|> ^
note: reference must be valid for the block
suffix following statement 0 at 2:16...
--> <anon>:2:17
2 |> let y: &i32;
|> ^
note: ...but borrowed value is only valid for
the block suffix following statement 0 at
4:18
--> <anon>:4:19
4 |> let x = 5;
|> ^
error: aborting due to previous error
compile
Borrowing
fn main() {
let y: &i32;
{
let x = 5;
y = &x;
}
println!("{}", y);
}
error: `x` does not live long enough
--> <anon>:5:14
5 |> y = &x;
|> ^
note: reference must be valid for the block
suffix following statement 0 at 2:16...
--> <anon>:2:17
2 |> let y: &i32;
|> ^
note: ...but borrowed value is only valid for
the block suffix following statement 0 at
4:18
--> <anon>:4:19
4 |> let x = 5;
|> ^
error: aborting due to previous error
compile
y = &x;
Lifetime of x is shorter than y.
Borrowing
fn main() {
let y: &i32;
let x = 5;
y = &x;
println!("{}", y);
}
error: `x` does not live long enough
--> <anon>:4:10
4 |> y = &x;
|> ^
note: reference must be valid for the block
suffix following statement 0 at 2:16...
--> <anon>:2:17
2 |> let y: &i32;
|> ^
note: ...but borrowed value is only valid for
the block suffix following statement 1 at
3:14
--> <anon>:3:15
3 |> let x = 5;
|> ^
error: aborting due to previous error
compile
Borrowing
fn main() {
let y: &i32;
let x = 5;
y = &x;
println!("{}", y);
}
error: `x` does not live long enough
--> <anon>:4:10
4 |> y = &x;
|> ^
note: reference must be valid for the block
suffix following statement 0 at 2:16...
--> <anon>:2:17
2 |> let y: &i32;
|> ^
note: ...but borrowed value is only valid for
the block suffix following statement 1 at
3:14
--> <anon>:3:15
3 |> let x = 5;
|> ^
error: aborting due to previous error
compile
y = &x;
Lifetime of x is shorter than y.
Borrowing
struct Pt { x: i32, y: i32 }
fn dot(v1: Pt, v2: Pt) -> (Pt, Pt) {
println!("{}", v1.x * v2.x +
v1.y * v2.y);
(v1, v2)
}
fn main() {
let v1 = Pt{ x: 3, y: 4 };
let v2 = Pt{ x: 1, y: 2 };
let (v1, v2) = dot(v1, v2);
println!("{} {}", v1.x, v1.y);
println!("{} {}", v2.x, v2.y);
}
struct Pt { x: i32, y: i32 }
fn dot(v1: &Pt, v2: &Pt) {
println!("{}", v1.x * v2.x +
v1.y * v2.y);
}
fn main() {
let v1 = Pt{ x: 3, y: 4 };
let v2 = Pt{ x: 1, y: 2 };
dot(&v1, &v2);
println!("{} {}", v1.x, v1.y);
println!("{} {}", v2.x, v2.y);
}
Borrowing
struct Pt { x: i32, y: i32 }
fn dot(v1: Pt, v2: Pt) -> (Pt, Pt) {
println!("{}", v1.x * v2.x +
v1.y * v2.y);
(v1, v2)
}
fn main() {
let v1 = Pt{ x: 3, y: 4 };
let v2 = Pt{ x: 1, y: 2 };
let (v1, v2) = dot(v1, v2);
println!("{} {}", v1.x, v1.y);
println!("{} {}", v2.x, v2.y);
}
struct Pt { x: i32, y: i32 }
fn dot(v1: &Pt, v2: &Pt) {
println!("{}", v1.x * v2.x +
v1.y * v2.y);
}
fn main() {
let v1 = Pt{ x: 3, y: 4 };
let v2 = Pt{ x: 1, y: 2 };
dot(&v1, &v2);
println!("{} {}", v1.x, v1.y);
println!("{} {}", v2.x, v2.y);
}
Mutable Borrowing
Use mutable references only if you need to change the values
you borrowed.
Only allow to borrow a mutable variables as a mutable
reference.
There is exactly one mutable reference to a variable.
A variable borrowed as a mutable reference can not be
borrowed as immutable references.
A variable borrowed as a mutable reference can not be used
until the end of borrowing.
Mutable Borrowing
fn main() {
let mut x = 0;
{
let y = &mut x;
*y += 1;
}
println!("x = {}", x);
}
x = 1
Program ended.
run
Mutable Borrowing
fn main() {
let mut x = 0;
{
let y = &mut x;
let z = &mut x;
*y += 1;
}
println!("x = {}", x);
}
error: cannot borrow `x` as mutable more than
once at a time [--explain E0499]
--> <anon>:5:22
4 |> let y = &mut x;
|> - first mutable
borrow occurs here
5 |> let z = &mut x;
|> ^ second mutable
borrow occurs here
6 |> *y += 1;
7 |> }
|> - first borrow ends here
error: aborting due to previous error
compile
Mutable Borrowing
fn main() {
let mut x = 0;
{
let y = &mut x;
let z = &mut x;
*y += 1;
}
println!("x = {}", x);
}
error: cannot borrow `x` as mutable more than
once at a time [--explain E0499]
--> <anon>:5:22
4 |> let y = &mut x;
|> - first mutable
borrow occurs here
5 |> let z = &mut x;
|> ^ second mutable
borrow occurs here
6 |> *y += 1;
7 |> }
|> - first borrow ends here
error: aborting due to previous error
compile
let z = &mut x;
Cannot borrow x as mutable reference more than once!
Mutable Borrowing
fn main() {
let mut x = 0;
{
let y = &mut x;
let z = &x;
*y += 1;
}
println!("x = {}", x);
}
error: cannot borrow `x` as immutable because
it is also borrowed as mutable [--explain
E0502]
--> <anon>:6:18
4 |> let y = &mut x;
|> - mutable borrow
occurs here
5 |> *y += 1;
6 |> let z = &x;
|> ^ immutable borrow
occurs here
7 |> }
|> - mutable borrow ends here
error: aborting due to previous error
compile
Mutable Borrowing
fn main() {
let mut x = 0;
{
let y = &mut x;
let z = &x;
*y += 1;
}
println!("x = {}", x);
}
error: cannot borrow `x` as immutable because
it is also borrowed as mutable [--explain
E0502]
--> <anon>:6:18
4 |> let y = &mut x;
|> - mutable borrow
occurs here
5 |> *y += 1;
6 |> let z = &x;
|> ^ immutable borrow
occurs here
7 |> }
|> - mutable borrow ends here
error: aborting due to previous error
compile
let z = &x;
Cannot borrow the variable been borrowed as a mutable reference!
Mutable Borrowing
fn main() {
let mut x = 0;
{
let y = &mut x;
let z = x + 1;
}
println!("x = {}", x);
}
error: cannot use `x` because it was mutably
borrowed [E0503]
--> <anon>:5:17
5 |> let z = x + 1;
|> ^
note: borrow of `x` occurs here
--> <anon>:4:22
4 |> let y = &mut x;
|> ^
error: aborting due to previous error
compile
Mutable Borrowing
fn main() {
let mut x = 0;
{
let y = &mut x;
let z = x + 1;
}
println!("x = {}", x);
}
error: cannot use `x` because it was mutably
borrowed [E0503]
--> <anon>:5:17
5 |> let z = x + 1;
|> ^
note: borrow of `x` occurs here
--> <anon>:4:22
4 |> let y = &mut x;
|> ^
error: aborting due to previous error
compile
let z = x + 1;
Cannot access the variable been borrowed as a mutable reference.
Thinking in Scopes
fn main() {
let mut x = 0;
let y = &mut x;
*y += 1;
println!("x = {}", x);
}
Why compile error?
Thinking in Scopes(cont’)
fn main() {
let mut x = 0;
let y = &mut x;
*y += 1;
println!("x = {}", x);
}
error: cannot borrow `x` as immutable because
it is also borrowed as mutable [--explain
E0502]
--> <anon>:5:24
3 |> let y = &mut x;
|> - mutable borrow occurs
here
4 |> *y += 1;
5 |> println!("x = {}", x);
|> ^ immutable
borrow occurs here
6 |> }
|> - mutable borrow ends here
<std macros>:2:27: 2:58: note: in this
expansion of format_args!
<std macros>:3:1: 3:54: note: in this
expansion of print! (defined in <std macros>)
<anon>:5:5: 5:27: note: in this expansion of
println! (defined in <std macros>)
error: aborting due to previous error
compile
Thinking in Scopes(cont’)
fn main() {
let mut x = 0;
let y = &mut x;
*y += 1;
println!("x = {}", x);
}
error: cannot borrow `x` as immutable because
it is also borrowed as mutable [--explain
E0502]
--> <anon>:5:24
3 |> let y = &mut x;
|> - mutable borrow occurs
here
4 |> *y += 1;
5 |> println!("x = {}", x);
|> ^ immutable
borrow occurs here
6 |> }
|> - mutable borrow ends here
<std macros>:2:27: 2:58: note: in this
expansion of format_args!
<std macros>:3:1: 3:54: note: in this
expansion of print! (defined in <std macros>)
<anon>:5:5: 5:27: note: in this expansion of
println! (defined in <std macros>)
error: aborting due to previous error
compile
println!("x = {}", x);
Immutable borrow occurs here!
Iterator Invalidation
fn main() {
let mut v = vec![1, 2, 3];
for i in &v {
println!("{}", i);
v.push(34);
}
}
error: cannot borrow `v` as mutable because
it is also borrowed as immutable [--explain
E0502]
--> <anon>:6:9
4 |> for i in &v {
|> - immutable borrow occurs
here
5 |> println!("{}", i);
6 |> v.push(34);
|> ^ mutable borrow occurs here
7 |> }
|> - immutable borrow ends here
error: aborting due to previous error
compile
Iterator Invalidation
fn main() {
let mut v = vec![1, 2, 3];
for i in &v {
println!("{}", i);
v.push(34);
}
}
error: cannot borrow `v` as mutable because
it is also borrowed as immutable [--explain
E0502]
--> <anon>:6:9
4 |> for i in &v {
|> - immutable borrow occurs
here
5 |> println!("{}", i);
6 |> v.push(34);
|> ^ mutable borrow occurs here
7 |> }
|> - immutable borrow ends here
error: aborting due to previous error
compile
v.push(34);
push(&mut self, …) try to borrow v as a mutable reference!
4.10 Lifetimes[link]
Syntax of Lifetimes Specifier
fn fn2<'a>(x: &'a i32) -> &'a i32 {
// do something
}
Syntax of Lifetimes Specifier
fn fn2<'a>(x: &'a i32) -> &'a i32 {
// do something
}
'a 'a 'a
input lifetime output lifetime
Syntax of Lifetimes Specifier
// lifetime with mutable reference
fn foo<'a>(x: &'a mut i32) -> &'a mut i32 {
// do something
}
// lifetime in struct
struct Foo<'a> {
x: &'a i32
}
// lifetime with impl
impl<'a> Foo<'a> {
fn x(&self) -> &'a i32 { self.x }
}
Lifetimes Specifier
All references need lifetimes.
Explicit lifetimes are used to make lifetime inference
unambiguous.
Must give explicit lifetimes for struct contain reference
members.
No need to give explicit lifetimes to the functions
without references return.
Lifetimes Specifier
struct Foo<'a> {
x: &'a i32
}
impl<'a> Foo<'a> {
fn x(&self) -> &'a i32 { self.x }
}
fn main() {
let y = 5;
let f = Foo { x: &y };
println!("{}", f.x);
println!("{}", f.x());
}
5
5
Program ended.
run
Lifetime Inference
Each elided lifetime of arguments becomes a distinct
lifetime parameter.
If there is exactly one input lifetime, all elided lifetimes
of the return values will be as same as the input
lifetime.
If there is &self in input lifetimes, all elided lifetimes of
the return values will be as same as &self.
Lifetime Inference (valid)
fn print(s: &str); // elided
fn print<'a>(s: &'a str); // expanded
//////////
fn substr(s: &str, until: u32) -> &str; // elided
fn substr<'a>(s: &'a str, until: u32) -> &'a str; // expanded
//////////
fn get_mut(&mut self) -> &mut T; // elided
fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded
Lifetime Inference (invalid)
fn get_str() -> &str; // ILLEGAL, no inputs
//////////
fn frob(s: &str, t: &str) -> &str; // Two input
lifetimes
fn frob<'a, 'b>(s: &'a str, t: &'b str) -> &str; // Output
lifetime is ambiguous
Static Lifetime
Some resources have the lifetime of the entire
program.
No need to give explicit lifetime for functions return
static resources.
Lifetimes Specifier
static FIVE: i32 = 5;
fn get_five() -> &'static i32 {
&FIVE
}
fn main() {
let x = get_five();
println!("x = {}", x);
}
x = 5
Program ended.
run
Thanks!
CC-BY-SA

More Related Content

What's hot

Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Introducing GitLab (June 2018)
Introducing GitLab (June 2018)
Noa Harel
 
Source Code Analysis with SAST
Source Code Analysis with SASTSource Code Analysis with SAST
Source Code Analysis with SAST
Blueinfy Solutions
 
Guaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in RustGuaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in Rust
nikomatsakis
 
Flexible and Real-Time Stream Processing with Apache Flink
Flexible and Real-Time Stream Processing with Apache FlinkFlexible and Real-Time Stream Processing with Apache Flink
Flexible and Real-Time Stream Processing with Apache Flink
DataWorks Summit
 
Scaling and High Performance Storage System: LeoFS
Scaling and High Performance Storage System: LeoFSScaling and High Performance Storage System: LeoFS
Scaling and High Performance Storage System: LeoFS
Rakuten Group, Inc.
 
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes DownDebugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
Aspen Mesh
 
FreeRTOS API
FreeRTOS APIFreeRTOS API
FreeRTOS API
Vincent Claes
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
Bo-Yi Wu
 
Temporal-Joins in Kafka Streams and ksqlDB | Matthias Sax, Confluent
Temporal-Joins in Kafka Streams and ksqlDB | Matthias Sax, ConfluentTemporal-Joins in Kafka Streams and ksqlDB | Matthias Sax, Confluent
Temporal-Joins in Kafka Streams and ksqlDB | Matthias Sax, Confluent
HostedbyConfluent
 
Container Runtime Security with Falco
Container Runtime Security with FalcoContainer Runtime Security with Falco
Container Runtime Security with Falco
Michael Ducy
 
Rust system programming language
Rust system programming languageRust system programming language
Rust system programming language
robin_sy
 
Nikto
NiktoNikto
Ransomware Resistance
Ransomware ResistanceRansomware Resistance
Ransomware Resistance
Florian Roth
 
user Behavior Analysis with Session Windows and Apache Kafka's Streams API
user Behavior Analysis with Session Windows and Apache Kafka's Streams APIuser Behavior Analysis with Session Windows and Apache Kafka's Streams API
user Behavior Analysis with Session Windows and Apache Kafka's Streams API
confluent
 
Android Multimedia Framework
Android Multimedia FrameworkAndroid Multimedia Framework
Android Multimedia Framework
Picker Weng
 
Rust vs C++
Rust vs C++Rust vs C++
Rust vs C++
corehard_by
 
Git ฉบับอนุบาล 2
Git ฉบับอนุบาล 2Git ฉบับอนุบาล 2
Git ฉบับอนุบาล 2
Bhuridech Sudsee
 
OFI Overview 2019 Webinar
OFI Overview 2019 WebinarOFI Overview 2019 Webinar
OFI Overview 2019 Webinar
seanhefty
 
Qemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System EmulationQemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System Emulation
National Cheng Kung University
 
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
HostedbyConfluent
 

What's hot (20)

Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Introducing GitLab (June 2018)
Introducing GitLab (June 2018)
 
Source Code Analysis with SAST
Source Code Analysis with SASTSource Code Analysis with SAST
Source Code Analysis with SAST
 
Guaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in RustGuaranteeing Memory Safety in Rust
Guaranteeing Memory Safety in Rust
 
Flexible and Real-Time Stream Processing with Apache Flink
Flexible and Real-Time Stream Processing with Apache FlinkFlexible and Real-Time Stream Processing with Apache Flink
Flexible and Real-Time Stream Processing with Apache Flink
 
Scaling and High Performance Storage System: LeoFS
Scaling and High Performance Storage System: LeoFSScaling and High Performance Storage System: LeoFS
Scaling and High Performance Storage System: LeoFS
 
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes DownDebugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
Debugging Your Debugging Tools: What to do When Your Service Mesh Goes Down
 
FreeRTOS API
FreeRTOS APIFreeRTOS API
FreeRTOS API
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
 
Temporal-Joins in Kafka Streams and ksqlDB | Matthias Sax, Confluent
Temporal-Joins in Kafka Streams and ksqlDB | Matthias Sax, ConfluentTemporal-Joins in Kafka Streams and ksqlDB | Matthias Sax, Confluent
Temporal-Joins in Kafka Streams and ksqlDB | Matthias Sax, Confluent
 
Container Runtime Security with Falco
Container Runtime Security with FalcoContainer Runtime Security with Falco
Container Runtime Security with Falco
 
Rust system programming language
Rust system programming languageRust system programming language
Rust system programming language
 
Nikto
NiktoNikto
Nikto
 
Ransomware Resistance
Ransomware ResistanceRansomware Resistance
Ransomware Resistance
 
user Behavior Analysis with Session Windows and Apache Kafka's Streams API
user Behavior Analysis with Session Windows and Apache Kafka's Streams APIuser Behavior Analysis with Session Windows and Apache Kafka's Streams API
user Behavior Analysis with Session Windows and Apache Kafka's Streams API
 
Android Multimedia Framework
Android Multimedia FrameworkAndroid Multimedia Framework
Android Multimedia Framework
 
Rust vs C++
Rust vs C++Rust vs C++
Rust vs C++
 
Git ฉบับอนุบาล 2
Git ฉบับอนุบาล 2Git ฉบับอนุบาล 2
Git ฉบับอนุบาล 2
 
OFI Overview 2019 Webinar
OFI Overview 2019 WebinarOFI Overview 2019 Webinar
OFI Overview 2019 Webinar
 
Qemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System EmulationQemu JIT Code Generator and System Emulation
Qemu JIT Code Generator and System Emulation
 
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
 

Viewers also liked

面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite MatchingChih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's AlgorithmChih-Hsuan Kuo
 
面試心得
面試心得面試心得
面試心得
澐 向
 
面試經驗分享
面試經驗分享面試經驗分享
面試經驗分享
俊儀 郭
 
簡易的面試心得分享
簡易的面試心得分享簡易的面試心得分享
簡易的面試心得分享Jack Wang
 
Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹
Alan Tsai
 
張忠謀自傳
張忠謀自傳張忠謀自傳
張忠謀自傳5045033
 
關於測試,我說的其實是......
關於測試,我說的其實是......關於測試,我說的其實是......
關於測試,我說的其實是......
hugo lu
 
吳明展的履歷 My Resume 2009 (ppt)
吳明展的履歷 My Resume 2009 (ppt)吳明展的履歷 My Resume 2009 (ppt)
吳明展的履歷 My Resume 2009 (ppt)
Anderson Wu, PMP, CSM, 吳明展
 

Viewers also liked (11)

面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching[ACM-ICPC] Bipartite Matching
[ACM-ICPC] Bipartite Matching
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
 
面試心得
面試心得面試心得
面試心得
 
面試經驗分享
面試經驗分享面試經驗分享
面試經驗分享
 
簡易的面試心得分享
簡易的面試心得分享簡易的面試心得分享
簡易的面試心得分享
 
Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹
 
張忠謀自傳
張忠謀自傳張忠謀自傳
張忠謀自傳
 
關於測試,我說的其實是......
關於測試,我說的其實是......關於測試,我說的其實是......
關於測試,我說的其實是......
 
吳明展的履歷 My Resume 2009 (ppt)
吳明展的履歷 My Resume 2009 (ppt)吳明展的履歷 My Resume 2009 (ppt)
吳明展的履歷 My Resume 2009 (ppt)
 

Similar to Ownership System in Rust

Rustlabs Quick Start
Rustlabs Quick StartRustlabs Quick Start
Rustlabs Quick Start
sangam biradar
 
Rust
RustRust
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017
pramode_ce
 
Rust Mozlando Tutorial
Rust Mozlando TutorialRust Mozlando Tutorial
Rust Mozlando Tutorial
nikomatsakis
 
Rust言語紹介
Rust言語紹介Rust言語紹介
Rust言語紹介
Paweł Rusin
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
Yandex
 
Ti1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and ScopesTi1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and ScopesEelco Visser
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Rust: код может быть одновременно безопасным и быстрым, Степан КольцовRust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Yandex
 
The Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideThe Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S Guide
Stephen Chin
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
Geeks Anonymes
 
Rust Intro @ Roma Rust meetup
Rust Intro @ Roma Rust meetup Rust Intro @ Roma Rust meetup
Rust Intro @ Roma Rust meetup
Claudio Capobianco
 
Torturing the PHP interpreter
Torturing the PHP interpreterTorturing the PHP interpreter
Torturing the PHP interpreter
Logicaltrust pl
 
[CONFidence 2016] Mateusz Kocielski - Torturing the PHP interpreter
[CONFidence 2016] Mateusz Kocielski - Torturing the PHP interpreter [CONFidence 2016] Mateusz Kocielski - Torturing the PHP interpreter
[CONFidence 2016] Mateusz Kocielski - Torturing the PHP interpreter
PROIDEA
 
What the &~#@&lt;!? (Pointers in Rust)
What the &~#@&lt;!? (Pointers in Rust)What the &~#@&lt;!? (Pointers in Rust)
What the &~#@&lt;!? (Pointers in Rust)
David Evans
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
Eduard Tomàs
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
Saifur Rahman
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
Leonardo Borges
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossumoscon2007
 
An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...
Claudio Capobianco
 

Similar to Ownership System in Rust (20)

Rustlabs Quick Start
Rustlabs Quick StartRustlabs Quick Start
Rustlabs Quick Start
 
Rust
RustRust
Rust
 
Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017 Rust Workshop - NITC FOSSMEET 2017
Rust Workshop - NITC FOSSMEET 2017
 
Rust Mozlando Tutorial
Rust Mozlando TutorialRust Mozlando Tutorial
Rust Mozlando Tutorial
 
Rust言語紹介
Rust言語紹介Rust言語紹介
Rust言語紹介
 
Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++Степан Кольцов — Rust — лучше, чем C++
Степан Кольцов — Rust — лучше, чем C++
 
Ti1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and ScopesTi1220 Lecture 2: Names, Bindings, and Scopes
Ti1220 Lecture 2: Names, Bindings, and Scopes
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Rust: код может быть одновременно безопасным и быстрым, Степан КольцовRust: код может быть одновременно безопасным и быстрым, Степан Кольцов
Rust: код может быть одновременно безопасным и быстрым, Степан Кольцов
 
The Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S GuideThe Java Fx Platform – A Java Developer’S Guide
The Java Fx Platform – A Java Developer’S Guide
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
Rust Intro @ Roma Rust meetup
Rust Intro @ Roma Rust meetup Rust Intro @ Roma Rust meetup
Rust Intro @ Roma Rust meetup
 
Torturing the PHP interpreter
Torturing the PHP interpreterTorturing the PHP interpreter
Torturing the PHP interpreter
 
[CONFidence 2016] Mateusz Kocielski - Torturing the PHP interpreter
[CONFidence 2016] Mateusz Kocielski - Torturing the PHP interpreter [CONFidence 2016] Mateusz Kocielski - Torturing the PHP interpreter
[CONFidence 2016] Mateusz Kocielski - Torturing the PHP interpreter
 
What the &~#@&lt;!? (Pointers in Rust)
What the &~#@&lt;!? (Pointers in Rust)What the &~#@&lt;!? (Pointers in Rust)
What the &~#@&lt;!? (Pointers in Rust)
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
 
Os Vanrossum
Os VanrossumOs Vanrossum
Os Vanrossum
 
An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...An introduction to Rust: the modern programming language to develop safe and ...
An introduction to Rust: the modern programming language to develop safe and ...
 

More from Chih-Hsuan Kuo

[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
Chih-Hsuan Kuo
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
Chih-Hsuan Kuo
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
Chih-Hsuan Kuo
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
Chih-Hsuan Kuo
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
Chih-Hsuan Kuo
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
Chih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
Chih-Hsuan Kuo
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
Chih-Hsuan Kuo
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
Chih-Hsuan Kuo
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
Chih-Hsuan Kuo
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
Chih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-upChih-Hsuan Kuo
 

More from Chih-Hsuan Kuo (20)

[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
 
[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245[ACM-ICPC] UVa-10245
[ACM-ICPC] UVa-10245
 
[ACM-ICPC] Sort
[ACM-ICPC] Sort[ACM-ICPC] Sort
[ACM-ICPC] Sort
 
[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm[ACM-ICPC] Efficient Algorithm
[ACM-ICPC] Efficient Algorithm
 
[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up[ACM-ICPC] Top-down & Bottom-up
[ACM-ICPC] Top-down & Bottom-up
 
[ACM-ICPC] About I/O
[ACM-ICPC] About I/O[ACM-ICPC] About I/O
[ACM-ICPC] About I/O
 

Recently uploaded

Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 

Recently uploaded (20)

Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 

Ownership System in Rust

  • 3. Variable & Memory A variable name is only a name. It’s possible that a variable name can not access any memory. When a variable is declared, Rust allocates memory in stack and heap (if need) for it. When the owner of resources is destroyed, ALL resources it owned would be released.
  • 4. Variable & Memory Stack Heap fn main() { let v = vec![1, 2, 3]; } dynamic memory static memory
  • 5. Variable & Memory Stack Heap fn main() { let v = vec![1, 2, 3]; } dynamic memory static memory When the variable is destroyed…
  • 6. fn main() { let v = vec![1, 2, 3]; } Variable & Memory Stack Heap dynamic memory static memory All related resources will be destroyed, too!
  • 7. Move By Default Assignment operator is move semantics by default. There is exactly one variable binding to any resource. Avoid data racing to guarantee data consistency.
  • 8. Move By Default struct Point { x: i32, y: i32 } fn main() { let v1 = Point{ x: 10, y: 20}; let v2 = v1; println!("{}", v1.x); } error: use of moved value: `v1.x` [--explain E0382] --> <anon>:9:20 8 |> let v2 = v1; |> -- value moved here 9 |> println!("{}", v1.x); |> ^^^^ value used here after move <std macros>:2:27: 2:58: note: in this expansion of format_args! <std macros>:3:1: 3:54: note: in this expansion of print! (defined in <std macros>) <anon>:9:5: 9:26: note: in this expansion of println! (defined in <std macros>) note: move occurs because `v1` has type `Point`, which does not implement the `Copy` trait error: aborting due to previous error compile
  • 9. Move By Default struct Point { x: i32, y: i32 } fn main() { let v1 = Point{ x: 10, y: 20}; let v2 = v1; println!("{}", v1.x); } error: use of moved value: `v1.x` [--explain E0382] --> <anon>:9:20 8 |> let v2 = v1; |> -- value moved here 9 |> println!("{}", v1.x); |> ^^^^ value used here after move <std macros>:2:27: 2:58: note: in this expansion of format_args! <std macros>:3:1: 3:54: note: in this expansion of print! (defined in <std macros>) <anon>:9:5: 9:26: note: in this expansion of println! (defined in <std macros>) note: move occurs because `v1` has type `Point`, which does not implement the `Copy` trait error: aborting due to previous error compile Use of moved value! v1.x
  • 10. Move By Default stack struct Point { x: i32, y: i32 } fn main() { let v1 = Point{ x: 10, y: 20}; let v2 = v1; println!("{}", v1.x); } Point { x = 10, y = 20 } names v1
  • 11. Move By Default stack struct Point { x: i32, y: i32 } fn main() { let v1 = Point{ x: 10, y: 20}; let v2 = v1; println!("{}", v1.x); } Point { x = 10, y = 20 } names v1 v2
  • 12. Copyable Type The types which implement Copy trait can make assignment operator be copy semantics. Allow to use the variable which be copied. All primitive types implement the Copy trait.
  • 13. Copyable Type fn main() { let v1 = 10; let v2 = v1; println!("v1 = {}", v1); println!("v2 = {}", v2); } v1 = 10 v2 = 10 Program ended. run
  • 14. Copyable Type fn main() { let v1 = 10; let v2 = v1; println!("v1 = {}", v1); println!("v2 = {}", v2); } stacknames v1 i32 { 10 }
  • 15. Copyable Type fn main() { let v1 = 10; let v2 = v1; println!("v1 = {}", v1); println!("v2 = {}", v2); } stacknames v1 i32 { 10 } v2 i32 { 10 }
  • 16. Parameter Passing Passing parameters is also move semantics by default (no Copy trait). Developers should return the ownership of parameters by themselves. Yes, you should return ten variables back if you pass ten parameters into a function. 😜
  • 17. Parameter Passing struct Pt { x: i32, y: i32 } fn dist(v: Pt) -> Pt { println!("{}", v.x * v.x + v.y * v.y); v } fn main() { let v = Pt{ x: 3, y: 4 }; let v = dist(v); println!("{} {}", v.x, v.y); } struct Pt { x: i32, y: i32 } fn dot(v1: Pt, v2: Pt) -> (Pt, Pt) { println!("{}", v1.x * v2.x + v1.y * v2.y); (v1, v2) } fn main() { let v1 = Pt{ x: 3, y: 4 }; let v2 = Pt{ x: 1, y: 2 }; let (v1, v2) = dot(v1, v2); println!("{} {}", v1.x, v1.y); println!("{} {}", v2.x, v2.y); } one parameter two parameters
  • 18. Parameter Passing struct Pt { x: i32 } fn square(v: Pt) { println!("{}", v.x * v.x); } fn main() { let v = Pt{ x: 3 }; square(v); println!("{}", v.x); } error: use of moved value: `v.x` [--explain E0382] --> <anon>:10:20 9 |> square(v); |> - value moved here 10 |> println!("{}", v.x); |> ^^^ value used here after move <std macros>:2:27: 2:58: note: in this expansion of format_args! <std macros>:3:1: 3:54: note: in this expansion of print! (defined in <std macros>) <anon>:10:5: 10:25: note: in this expansion of println! (defined in <std macros>) note: move occurs because `v` has type `Pt`, which does not implement the `Copy` trait error: aborting due to previous error compile
  • 19. Parameter Passing struct Pt { x: i32 } fn square(v: Pt) { println!("{}", v.x * v.x); } fn main() { let v = Pt{ x: 3 }; square(v); println!("{}", v.x); } error: use of moved value: `v.x` [--explain E0382] --> <anon>:10:20 9 |> square(v); |> - value moved here 10 |> println!("{}", v.x); |> ^^^ value used here after move <std macros>:2:27: 2:58: note: in this expansion of format_args! <std macros>:3:1: 3:54: note: in this expansion of print! (defined in <std macros>) <anon>:10:5: 10:25: note: in this expansion of println! (defined in <std macros>) note: move occurs because `v` has type `Pt`, which does not implement the `Copy` trait error: aborting due to previous error compile v.x Use of moved value!
  • 20. 4.9 Reference and Borrowing[link]
  • 21. Syntax of Reference fn main() { let a = 1; let b = &a; // &a is the reference to a let mut c = 2; let d = &mut c; // &mut c is the mutable reference to c }
  • 22. Borrowing Use the references to borrow the ownership. The ownership will return to original owner when the borrower is destroyed automatically. References are immutable. Allow multiple references to one variable. A borrowed variable can be read but not written. Only allow to borrow the variable with longer lifetime.
  • 23. Borrowing fn main() { let orig = 0; let b1 = &orig; let b2 = &orig; let b3 = &orig; println!("b1 = {}", b1); println!("b2 = {}", b2); println!("b3 = {}", b3); println!("orig = {}", orig); } b1 = 0 b2 = 0 b3 = 0 orig = 0 Program ended. run
  • 24. Borrowing fn main() { let mut x = 0; { let y = &x; x += 1; println!("{}", y); } println!("{}", x); } error: cannot assign to `x` because it is borrowed [--explain E0506] --> <anon>:5:9 4 |> let y = &x; |> - borrow of `x` occurs here 5 |> x += 1; |> ^^^^^^ assignment to borrowed `x` occurs here error: aborting due to previous error compile
  • 25. Borrowing fn main() { let mut x = 0; { let y = &x; x += 1; println!("{}", y); } println!("{}", x); } error: cannot assign to `x` because it is borrowed [--explain E0506] --> <anon>:5:9 4 |> let y = &x; |> - borrow of `x` occurs here 5 |> x += 1; |> ^^^^^^ assignment to borrowed `x` occurs here error: aborting due to previous error compile x += 1; Cannot write the borrowed variable!
  • 26. Borrowing fn main() { let y: &i32; { let x = 5; y = &x; } println!("{}", y); } error: `x` does not live long enough --> <anon>:5:14 5 |> y = &x; |> ^ note: reference must be valid for the block suffix following statement 0 at 2:16... --> <anon>:2:17 2 |> let y: &i32; |> ^ note: ...but borrowed value is only valid for the block suffix following statement 0 at 4:18 --> <anon>:4:19 4 |> let x = 5; |> ^ error: aborting due to previous error compile
  • 27. Borrowing fn main() { let y: &i32; { let x = 5; y = &x; } println!("{}", y); } error: `x` does not live long enough --> <anon>:5:14 5 |> y = &x; |> ^ note: reference must be valid for the block suffix following statement 0 at 2:16... --> <anon>:2:17 2 |> let y: &i32; |> ^ note: ...but borrowed value is only valid for the block suffix following statement 0 at 4:18 --> <anon>:4:19 4 |> let x = 5; |> ^ error: aborting due to previous error compile y = &x; Lifetime of x is shorter than y.
  • 28. Borrowing fn main() { let y: &i32; let x = 5; y = &x; println!("{}", y); } error: `x` does not live long enough --> <anon>:4:10 4 |> y = &x; |> ^ note: reference must be valid for the block suffix following statement 0 at 2:16... --> <anon>:2:17 2 |> let y: &i32; |> ^ note: ...but borrowed value is only valid for the block suffix following statement 1 at 3:14 --> <anon>:3:15 3 |> let x = 5; |> ^ error: aborting due to previous error compile
  • 29. Borrowing fn main() { let y: &i32; let x = 5; y = &x; println!("{}", y); } error: `x` does not live long enough --> <anon>:4:10 4 |> y = &x; |> ^ note: reference must be valid for the block suffix following statement 0 at 2:16... --> <anon>:2:17 2 |> let y: &i32; |> ^ note: ...but borrowed value is only valid for the block suffix following statement 1 at 3:14 --> <anon>:3:15 3 |> let x = 5; |> ^ error: aborting due to previous error compile y = &x; Lifetime of x is shorter than y.
  • 30. Borrowing struct Pt { x: i32, y: i32 } fn dot(v1: Pt, v2: Pt) -> (Pt, Pt) { println!("{}", v1.x * v2.x + v1.y * v2.y); (v1, v2) } fn main() { let v1 = Pt{ x: 3, y: 4 }; let v2 = Pt{ x: 1, y: 2 }; let (v1, v2) = dot(v1, v2); println!("{} {}", v1.x, v1.y); println!("{} {}", v2.x, v2.y); } struct Pt { x: i32, y: i32 } fn dot(v1: &Pt, v2: &Pt) { println!("{}", v1.x * v2.x + v1.y * v2.y); } fn main() { let v1 = Pt{ x: 3, y: 4 }; let v2 = Pt{ x: 1, y: 2 }; dot(&v1, &v2); println!("{} {}", v1.x, v1.y); println!("{} {}", v2.x, v2.y); }
  • 31. Borrowing struct Pt { x: i32, y: i32 } fn dot(v1: Pt, v2: Pt) -> (Pt, Pt) { println!("{}", v1.x * v2.x + v1.y * v2.y); (v1, v2) } fn main() { let v1 = Pt{ x: 3, y: 4 }; let v2 = Pt{ x: 1, y: 2 }; let (v1, v2) = dot(v1, v2); println!("{} {}", v1.x, v1.y); println!("{} {}", v2.x, v2.y); } struct Pt { x: i32, y: i32 } fn dot(v1: &Pt, v2: &Pt) { println!("{}", v1.x * v2.x + v1.y * v2.y); } fn main() { let v1 = Pt{ x: 3, y: 4 }; let v2 = Pt{ x: 1, y: 2 }; dot(&v1, &v2); println!("{} {}", v1.x, v1.y); println!("{} {}", v2.x, v2.y); }
  • 32. Mutable Borrowing Use mutable references only if you need to change the values you borrowed. Only allow to borrow a mutable variables as a mutable reference. There is exactly one mutable reference to a variable. A variable borrowed as a mutable reference can not be borrowed as immutable references. A variable borrowed as a mutable reference can not be used until the end of borrowing.
  • 33. Mutable Borrowing fn main() { let mut x = 0; { let y = &mut x; *y += 1; } println!("x = {}", x); } x = 1 Program ended. run
  • 34. Mutable Borrowing fn main() { let mut x = 0; { let y = &mut x; let z = &mut x; *y += 1; } println!("x = {}", x); } error: cannot borrow `x` as mutable more than once at a time [--explain E0499] --> <anon>:5:22 4 |> let y = &mut x; |> - first mutable borrow occurs here 5 |> let z = &mut x; |> ^ second mutable borrow occurs here 6 |> *y += 1; 7 |> } |> - first borrow ends here error: aborting due to previous error compile
  • 35. Mutable Borrowing fn main() { let mut x = 0; { let y = &mut x; let z = &mut x; *y += 1; } println!("x = {}", x); } error: cannot borrow `x` as mutable more than once at a time [--explain E0499] --> <anon>:5:22 4 |> let y = &mut x; |> - first mutable borrow occurs here 5 |> let z = &mut x; |> ^ second mutable borrow occurs here 6 |> *y += 1; 7 |> } |> - first borrow ends here error: aborting due to previous error compile let z = &mut x; Cannot borrow x as mutable reference more than once!
  • 36. Mutable Borrowing fn main() { let mut x = 0; { let y = &mut x; let z = &x; *y += 1; } println!("x = {}", x); } error: cannot borrow `x` as immutable because it is also borrowed as mutable [--explain E0502] --> <anon>:6:18 4 |> let y = &mut x; |> - mutable borrow occurs here 5 |> *y += 1; 6 |> let z = &x; |> ^ immutable borrow occurs here 7 |> } |> - mutable borrow ends here error: aborting due to previous error compile
  • 37. Mutable Borrowing fn main() { let mut x = 0; { let y = &mut x; let z = &x; *y += 1; } println!("x = {}", x); } error: cannot borrow `x` as immutable because it is also borrowed as mutable [--explain E0502] --> <anon>:6:18 4 |> let y = &mut x; |> - mutable borrow occurs here 5 |> *y += 1; 6 |> let z = &x; |> ^ immutable borrow occurs here 7 |> } |> - mutable borrow ends here error: aborting due to previous error compile let z = &x; Cannot borrow the variable been borrowed as a mutable reference!
  • 38. Mutable Borrowing fn main() { let mut x = 0; { let y = &mut x; let z = x + 1; } println!("x = {}", x); } error: cannot use `x` because it was mutably borrowed [E0503] --> <anon>:5:17 5 |> let z = x + 1; |> ^ note: borrow of `x` occurs here --> <anon>:4:22 4 |> let y = &mut x; |> ^ error: aborting due to previous error compile
  • 39. Mutable Borrowing fn main() { let mut x = 0; { let y = &mut x; let z = x + 1; } println!("x = {}", x); } error: cannot use `x` because it was mutably borrowed [E0503] --> <anon>:5:17 5 |> let z = x + 1; |> ^ note: borrow of `x` occurs here --> <anon>:4:22 4 |> let y = &mut x; |> ^ error: aborting due to previous error compile let z = x + 1; Cannot access the variable been borrowed as a mutable reference.
  • 40. Thinking in Scopes fn main() { let mut x = 0; let y = &mut x; *y += 1; println!("x = {}", x); } Why compile error?
  • 41. Thinking in Scopes(cont’) fn main() { let mut x = 0; let y = &mut x; *y += 1; println!("x = {}", x); } error: cannot borrow `x` as immutable because it is also borrowed as mutable [--explain E0502] --> <anon>:5:24 3 |> let y = &mut x; |> - mutable borrow occurs here 4 |> *y += 1; 5 |> println!("x = {}", x); |> ^ immutable borrow occurs here 6 |> } |> - mutable borrow ends here <std macros>:2:27: 2:58: note: in this expansion of format_args! <std macros>:3:1: 3:54: note: in this expansion of print! (defined in <std macros>) <anon>:5:5: 5:27: note: in this expansion of println! (defined in <std macros>) error: aborting due to previous error compile
  • 42. Thinking in Scopes(cont’) fn main() { let mut x = 0; let y = &mut x; *y += 1; println!("x = {}", x); } error: cannot borrow `x` as immutable because it is also borrowed as mutable [--explain E0502] --> <anon>:5:24 3 |> let y = &mut x; |> - mutable borrow occurs here 4 |> *y += 1; 5 |> println!("x = {}", x); |> ^ immutable borrow occurs here 6 |> } |> - mutable borrow ends here <std macros>:2:27: 2:58: note: in this expansion of format_args! <std macros>:3:1: 3:54: note: in this expansion of print! (defined in <std macros>) <anon>:5:5: 5:27: note: in this expansion of println! (defined in <std macros>) error: aborting due to previous error compile println!("x = {}", x); Immutable borrow occurs here!
  • 43. Iterator Invalidation fn main() { let mut v = vec![1, 2, 3]; for i in &v { println!("{}", i); v.push(34); } } error: cannot borrow `v` as mutable because it is also borrowed as immutable [--explain E0502] --> <anon>:6:9 4 |> for i in &v { |> - immutable borrow occurs here 5 |> println!("{}", i); 6 |> v.push(34); |> ^ mutable borrow occurs here 7 |> } |> - immutable borrow ends here error: aborting due to previous error compile
  • 44. Iterator Invalidation fn main() { let mut v = vec![1, 2, 3]; for i in &v { println!("{}", i); v.push(34); } } error: cannot borrow `v` as mutable because it is also borrowed as immutable [--explain E0502] --> <anon>:6:9 4 |> for i in &v { |> - immutable borrow occurs here 5 |> println!("{}", i); 6 |> v.push(34); |> ^ mutable borrow occurs here 7 |> } |> - immutable borrow ends here error: aborting due to previous error compile v.push(34); push(&mut self, …) try to borrow v as a mutable reference!
  • 46. Syntax of Lifetimes Specifier fn fn2<'a>(x: &'a i32) -> &'a i32 { // do something }
  • 47. Syntax of Lifetimes Specifier fn fn2<'a>(x: &'a i32) -> &'a i32 { // do something } 'a 'a 'a input lifetime output lifetime
  • 48. Syntax of Lifetimes Specifier // lifetime with mutable reference fn foo<'a>(x: &'a mut i32) -> &'a mut i32 { // do something } // lifetime in struct struct Foo<'a> { x: &'a i32 } // lifetime with impl impl<'a> Foo<'a> { fn x(&self) -> &'a i32 { self.x } }
  • 49. Lifetimes Specifier All references need lifetimes. Explicit lifetimes are used to make lifetime inference unambiguous. Must give explicit lifetimes for struct contain reference members. No need to give explicit lifetimes to the functions without references return.
  • 50. Lifetimes Specifier struct Foo<'a> { x: &'a i32 } impl<'a> Foo<'a> { fn x(&self) -> &'a i32 { self.x } } fn main() { let y = 5; let f = Foo { x: &y }; println!("{}", f.x); println!("{}", f.x()); } 5 5 Program ended. run
  • 51. Lifetime Inference Each elided lifetime of arguments becomes a distinct lifetime parameter. If there is exactly one input lifetime, all elided lifetimes of the return values will be as same as the input lifetime. If there is &self in input lifetimes, all elided lifetimes of the return values will be as same as &self.
  • 52. Lifetime Inference (valid) fn print(s: &str); // elided fn print<'a>(s: &'a str); // expanded ////////// fn substr(s: &str, until: u32) -> &str; // elided fn substr<'a>(s: &'a str, until: u32) -> &'a str; // expanded ////////// fn get_mut(&mut self) -> &mut T; // elided fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded
  • 53. Lifetime Inference (invalid) fn get_str() -> &str; // ILLEGAL, no inputs ////////// fn frob(s: &str, t: &str) -> &str; // Two input lifetimes fn frob<'a, 'b>(s: &'a str, t: &'b str) -> &str; // Output lifetime is ambiguous
  • 54. Static Lifetime Some resources have the lifetime of the entire program. No need to give explicit lifetime for functions return static resources.
  • 55. Lifetimes Specifier static FIVE: i32 = 5; fn get_five() -> &'static i32 { &FIVE } fn main() { let x = get_five(); println!("x = {}", x); } x = 5 Program ended. run