13. fn main() {
let n = 5;
if n < 0 {
println!("{} is negative", n);
} else if n > 0 {
println!("{} is positive", n);
} else {
println!("{} is zero", n);
}
}
example2.rs
Non-mutable!
14. struct Rectangle{
x : i32,
y : i32
}
impl Rectangle{
fn area(&self) -> i32{
self.x*self.y
}
}
fn main() {
let rect = Rectangle{x:2,y:3};
println!("Rectangle area is {}",rect.area());
}
example3.rs (OO
P) struct定義でIV
struct実装でメソッド
16. example4.rs (純関数型)
Rust is fun!
You can use High Order Functions!
I want to learn more!
12
33
21
61
sum()
String.len()
String.len()
String.len()
map()
18. borrow.rs (メモリー管理)
fn main() {
let x = 4;
let y = &x;
println!("Address of x is {:p} address of y is
{:p}",&x,y);
}
Address of x is 0x7fff5fa1795c address of y is 0x7fff5fa1795c
20. example5.rs (メモリー管
理)
fn main() {
let x = 4;
let y = &x;
println!("Address of x is {:p} address of y is
{:p}",&x,y);
*y = 1;
}
borrow.rs:6:4: 6:10 error: cannot assign to immutable borrowed content
`*y`
borrow.rs:6 *y = 1;
^~~~~~
error: aborting due to previous error
21. example5.rs (メモリー管
理)
fn main() {
let mut x = 4;
let y = &x;
println!("Address of x is {:p} address of y is
{:p}",&x,y);
*y = 1;
}
borrow.rs:6:4: 6:10 error: cannot assign to immutable borrowed content
`*y`
borrow.rs:6 *y = 1;
^~~~~~
error: aborting due to previous error
22. example5.rs (メモリー管
理)
fn main() {
let mut x = 4;
let y = &mut x;
println!("Address of x is {:p} address of y is
{:p}",&x,y);
*y = 1;
}
borrow.rs:5:58: 5:59 error: cannot borrow `x` as immutable because it is
also borrowed as mutable
borrow.rs:5 println!("Address of x is {:p} address of y is {:p}",&x,y);
^
note: in expansion of format_args!
<std macros>:2:25: 2:58 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
borrow.rs:5:4: 5:63 note: expansion site
borrow.rs:4:17: 4:18 note: previous borrow of `x` occurs here; the mutable
borrow prevents subsequent moves, borrows, or modification of `x` until
the borrow ends
borrow.rs:4 let y = &mut x;
^
borrow.rs:7:2: 7:2 note: previous borrow ends here
borrow.rs:1 fn main() {
...
borrow.rs:7 }
^
error: aborting due to previous error
23. example5.rs (メモリー管
理)
fn main() {
let mut x = 4;
let y = &mut x;
println!("Address of x is {:p} address of y is
{:p}",&x,y);
*y = 1;
}
borrow.rs:5:58: 5:59 error: cannot borrow `x` as immutable because it is
also borrowed as mutable
borrow.rs:5 println!("Address of x is {:p} address of y is {:p}",&x,y);
^
note: in expansion of format_args!
<std macros>:2:25: 2:58 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
borrow.rs:5:4: 5:63 note: expansion site
borrow.rs:4:17: 4:18 note: previous borrow of `x` occurs here; the mutable
borrow prevents subsequent moves, borrows, or modification of `x` until
the borrow ends
borrow.rs:4 let y = &mut x;
^
borrow.rs:7:2: 7:2 note: previous borrow ends here
borrow.rs:1 fn main() {
...
borrow.rs:7 }
^
error: aborting due to previous error
25. example5.rs (メモリー管
理)
fn main() {
let mut x = 4;
if true {
let y = &mut x;
println!("Address of y is {:p}",y);
*y = 1;
}
println!("Address of x is {:p}",&x);
}
Address of y is 0x7fff51b4795c
Address of x is 0x7fff51b4795c
yのlifetime = 借りる範囲
28. example5.rs (メモリー管
理)
fn main() {
let x = Box::new(4);
let y = x;
println!("Value of y is {}",y);
println!("Value of x is {}",x);
}
borrow.rs:6:32: 6:33 error: use of moved value: `x`
borrow.rs:6 println!("Value of x is {}",x);
^
note: in expansion of format_args!
<std macros>:2:25: 2:58 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
borrow.rs:6:4: 6:35 note: expansion site
borrow.rs:4:8: 4:9 note: `x` moved here because it has type
`Box<i32>`, which is moved by default
borrow.rs:4 let y = x;
^
borrow.rs:4:9: 4:9 help: use `ref` to override
error: aborting due to previous error
29. example5.rs (メモリー管
理)fn main() {
let x = Box::new(4);
println!("Value of x is {}",x);
if true {
let y = x;
println!("Value of y is {}",y);
}
println!("Value of x is {}",x);
}
borrow.rs:11:35: 11:36 error: use of moved value: `x`
borrow.rs:11 println!("Value of x is {}",x);
^
note: in expansion of format_args!
<std macros>:2:25: 2:58 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
borrow.rs:11:7: 11:38 note: expansion site
borrow.rs:8:11: 8:12 note: `x` moved here because it has type `Box<i32>`,
which is moved by default
borrow.rs:8 let y = x;
^
borrow.rs:8:12: 8:12 help: use `ref` to override
error: aborting due to previous error
yのlifetime
30. example5.rs (メモリー管
理)fn main() {
let x = Box::new(4);
println!("Value of x is {}",x);
if true {
let y = x;
println!("Value of y is {}",y);
x = y;
}
println!("Value of x is {}",x);
}
borrow.rs:11:35: 11:36 error: use of moved value: `x`
borrow.rs:11 println!("Value of x is {}",x);
^
note: in expansion of format_args!
<std macros>:2:25: 2:58 note: expansion site
<std macros>:1:1: 2:62 note: in expansion of print!
<std macros>:3:1: 3:54 note: expansion site
<std macros>:1:1: 3:58 note: in expansion of println!
borrow.rs:11:7: 11:38 note: expansion site
borrow.rs:8:11: 8:12 note: `x` moved here because it has type `Box<i32>`,
which is moved by default
borrow.rs:8 let y = x;
^
borrow.rs:8:12: 8:12 help: use `ref` to override
error: aborting due to previous error
31. example5.rs (メモリー管
理)fn main() {
let mut x = Box::new(4);
println!("Value of x is {}",x);
if true {
let y = x;
println!("Value of y is {}",y);
x = y;
}
println!("Value of x is {}",x);
}
Value of x is 4
Value of y is 4
Value of x is 4