2020/08/21 堀川由人
プレゼン20分くらい+議論10分位で18:30前後終了を目指します。
構造体を定義する
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool, // ここの,は無くてもok
}
インスタンスをつくる(順番は気にしなくてOK)
let user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
エラーになる例
let user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
user1.active = false
ここでもフィールドを変更する場合はmut
が必要
let mut user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
user1.active = false
2つのString
引数を取ってUser
を返す
fn build_user(email: String, username: String) -> User {
User {
email: email,
username: username,
active: true,
sign_in_count: 1,
}
}
実は省略可能
fn build_user(email: String, username: String) -> User {
User {
email,
username,
active: true,
sign_in_count: 1,
}
}
他のuser2
を定義. ただし足りないものはuser1
から
let user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
..user1
};
引数の順番のみが重要な場合
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
(個人的にはRGBかBGRを明記したいので避けたい)
fn main() {
let width1 = 30;
let height1 = 50;
println!("The area is {}.", area(width1, height1));
}
fn area(width: u32, height: u32) -> u32 {
width * height
}
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
println!("The area is {}.", area(&rect1));
}
fn area(rectangle: &Rectangle) -> u32 {
rectangle.width * rectangle.height
}
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
println!("rect1 is {}", rect1);
}
これはエラー(標準的なRectangle->Stringが無い)
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
println!("rect1 is {:?}", rect1);
}
#[derive(Debug)]
でRectangleにデバッグ宣言{:?}
で構造体をよしなに文字列に変換#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
println!("The area is {}.", rect1.area());
}
impl <struct>
内にメソッドを定義
implement: 実装
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn perimeter(&self) -> u32 {
2 * self.width + 2 * self.height
}
}
impl Rectangle {
fn diagonal(&self) -> f64 {
((self.width * self.width + self.height * self.height) as f64).sqrt()
}
}
分かれててもok
impl Rectangle {
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
fn main() {
let rect1 = Rectangle {width: 30, height: 50 };
let rect2 = Rectangle {width: 10, height: 40 };
let rect3 = Rectangle {width: 60, height: 45 };
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); // true
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); // false
}
impl Rectangle {
fn square(size: u32) -> Rectangle {
Rectangle { width: size, height: size }
}
}
let sq = Rectangle::square(3);
として正方形が定義できる.
関連関数はメソッドとは呼ばれない
struct
impl
mut
が必要(Rustでは当たり前なのだとと思いますが)Juliaでは
mutable struct hoge end
struct fuga end
によってfieldの変更可能/不能な構造体を分けていたので少し意外に感じました