Kenta Arai
2021/8/20
enum IpAddrKind {
V4,
V6,
}
let four = IpAddrKind::V4;
let six = IpAddrKind::V6;
列挙型名::列挙子名
let four = IpAddrKind::V4;
let six: IpAddrKind = IpAddrKind::V6;
fn route(ip_type: IpAddrKind) { /* 省略 */ }
enum IpAddr {
V4(String),
V6(String),
}
let home = IpAddr::V4(String::from("127.0.0.1"));
let loopback = IpAddr::V6(String::from("::1"));
enum IpAddr {
V4(u8, u8, u8, u8), // IPv4は個別の値を保持したいな
V6(String), // IPv6は長いから文字列でいいや
}
let home = IpAddr::V4(127, 0, 0, 1);
let loopback = IpAddr::V6(String::from("::1"));
struct Ipv4Addr { /* 省略 */ }
struct Ipv6Addr { /* 省略 */ }
enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
struct QuitMessage; // ユニット構造体
struct MoveMessage {
x: i32,
y: i32,
}
struct WriteMessage(String); // タプル構造体
struct ChangeColorMessage(i32, i32, i32); // タプル構造体
impl Message {
fn call(&self) {
// メソッド本体はここに定義される
}
}
let m = Message::Write(String::from("hello"));
m.call();
enum Option<T> {
Some(T),
None,
}
let some_number = Some(5);
let some_string = Some("a string");
let absent_number: Option<i32> = None;
let x: i8 = 5;
let y: Option<i8> = Some(3);
let sum = x + y;
Compiling playground v0.0.1 (/playground)
error[E0277]: cannot add `Option<i8>` to `i8`
--> src/main.rs:6:17
|
6 | let sum = x + y;
| ^ no implementation for `i8 + Option<i8>`
|
= help: the trait `Add<Option<i8>>` is not implemented for `i8`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
let x: i8 = 5;
let y: Option<i8> = Some(3);
let sum = x + y.unwrap();
match
は式であるため,値を返すenum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u32 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
fn value_in_cents(coin: Coin) -> u32 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
// Coin::Quarter => 25,
}
}
Compiling playground v0.0.1 (/playground)
error[E0004]: non-exhaustive patterns: `Quarter` not covered
--> src/main.rs:9:11
|
1 | / enum Coin {
2 | | Penny,
3 | | Nickel,
4 | | Dime,
5 | | Quarter,
| | ------- not covered
6 | | }
| |_- `Coin` defined here
...
9 | match coin {
| ^^^^ pattern `Quarter` not covered
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
= note: the matched value is of type `Coin`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0004`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
let some_u8_value = 0u8;
match some_u8_value {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}
match
はマッチした値を束縛できるPatterns that Bind to Values
#[derive(Debug)] // すぐに州を点検できるように
enum UsState {
Alabama,
Alaska,
// ... などなど
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u32 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
},
}
}
match
で扱えますfn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
match
は少し長いif let
はより短い表現で値をマッチさせることができます// matchだとちょっと長い
let some_u8_value = Some(0u8);
match some_u8_value {
Some(3) => println!("three"),
_ => (),
}
// if letだと簡潔
if let Some(3) = some_u8_value {
println!("three");
}