Rustlings でエクササイズ

リポジトリ
解答

Intro
intro1.rs
- // I AM NOT DONE
コメントアウトを削除。
参考
intro2.rs
println!("Hello there!")
println!
に変更。
参考

Variables
variables1.rs
fn main() {
let x = 5;
println!("x has the value {}", x);
}
let
で変数を宣言。
参考
- https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#variables-and-mutability
- https://doc.rust-lang.org/rust-by-example/custom_types/constants.html
variables2.rs
fn main() {
let x = 10;
if x == 10 {
println!("x is ten!");
} else {
println!("x is not ten!");
}
}
x
を10
と比較するために初期化が必要。
参考
variables3.rs
fn main() {
let x: i32 = 0;
println!("Number {}", x);
}
x
が宣言だけされて初期化されていないのでi32
型の値で初期化する。
参考
variables4.rs
fn main() {
let mut x = 3;
println!("Number {}", x);
x = 5; // don't change this line
println!("Number {}", x);
}
Rustの変数はデフォルトで不変である。可変にするためにmut
キーワードを使用する。
参考
- https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#variables-and-mutability
- https://doc.rust-lang.org/rust-by-example/variable_bindings/mut.html
variables5.rs
fn main() {
let number = "T-H-R-E-E"; // don't change this line
println!("Spell a Number : {}", number);
let number = 3; // don't rename this variable
println!("Number plus two is : {}", number + 2);
}
以前の変数と同じ変数名で、変数を宣言することができる。これをShadowingという。
参考
- https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
- https://doc.rust-lang.org/rust-by-example/variable_bindings/scope.html
variables6.rs
const NUMBER: i32 = 3;
fn main() {
println!("Number {}", NUMBER);
}
const
で定数を宣言できる。定数の場合は必ず型注釈をつける必要がある。
参考

Functions
functions1.rs
fn main() {
call_me();
}
fn call_me() {}
関数の宣言はfn
キーワードを使用して行う。
参考
- https://doc.rust-lang.org/book/ch03-03-how-functions-work.html#functions
- https://doc.rust-lang.org/rust-by-example/fn.html
functions2.rs
fn main() {
call_me(3);
}
fn call_me(num: i32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
// Ring! Call number 1
// Ring! Call number 2
// Ring! Call number 3
関数シグネチャでは、各引数に型を指定する。i32
を指定しているが、3
が含まれる型であれば何でも良い。
参考
- https://doc.rust-lang.org/book/ch03-03-how-functions-work.html#parameters
- https://doc.rust-lang.org/rust-by-example/fn.html
functions3.rs
fn main() {
call_me(2);
}
fn call_me(num: u32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
// Ring! Call number 1
// Ring! Call number 2
call_me
の実行時に引数を渡していなかったので、u32
の引数を渡す。
参考
- https://doc.rust-lang.org/book/ch03-03-how-functions-work.html#parameters
- https://doc.rust-lang.org/rust-by-example/fn.html
functions4.rs
fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}
fn sale_price(price: i32) -> i32 {
if is_even(price) {
price - 10
} else {
price - 3
}
}
fn is_even(num: i32) -> bool {
num % 2 == 0
}
// Your sale price is 48
戻り値ががある場合は、戻り値の型も指定する必要がある。
参考
- https://doc.rust-lang.org/book/ch03-03-how-functions-work.html#functions-with-return-values
- https://doc.rust-lang.org/rust-by-example/fn.html
functions5.rs
fn main() {
let answer = square(3);
println!("The square of 3 is {}", answer);
}
fn square(num: i32) -> i32 {
num * num
}
// The square of 3 is 9
return
キーワードを省略することで最後の式が戻り値となる。num * num;
のようにセミコロンを付けてしまうと、式が文になってしまい、文は値を返さないのでエラーとなる。
参考

If
if1.rs
pub fn bigger(a: i32, b: i32) -> i32 {
// Complete this function to return the bigger number!
// Do not use:
// - another function call
// - additional variables
if a > b {
a
} else {
b
}
}
if
式を使って、a
とb
のうち大きい方を返す関数を作成する。
参考
- https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions
- https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html
if2.rs
pub fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else if fizzish == "fuzz" {
"bar"
} else {
"baz"
}
}
else if
で複数の条件を持たせることができる。
参考
- https://doc.rust-lang.org/book/ch03-05-control-flow.html#handling-multiple-conditions-with-else-if
- https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html
if3.rs
pub fn animal_habitat(animal: &str) -> &'static str {
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
2
} else if animal == "snake" {
3
} else {
4
};
// DO NOT CHANGE THIS STATEMENT BELOW
let habitat = if identifier == 1 {
"Beach"
} else if identifier == 2 {
"Burrow"
} else if identifier == 3 {
"Desert"
} else {
"Unknown"
};
habitat
}
if
は式であるため、let
文の右辺に使うことができる。その場合、返す値の型が一致している必要がある。
参考

quiz1
fn calculate_price_of_apples(num: u32) -> u32 {
let apple_unit_price = if num > 40 { 1 } else { 2 };
let price = num * apple_unit_price;
price
}
// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
上記の条件で計算する。

Primitive Types
primitive_types1.rs
fn main() {
// Booleans (`bool`)
let is_morning = true;
if is_morning {
println!("Good morning!");
}
let is_evening = !is_morning; // Finish the rest of this line like the example! Or make it be false!
if is_evening {
println!("Good evening!");
}
}
// Good morning!
is_evening
はis_morning
と条件を反転させる。
参考
- https://doc.rust-lang.org/stable/book/ch03-02-data-types.html#the-boolean-type
- https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html#if-expressions
- https://doc.rust-lang.org/rust-by-example/primitives/literals.html
- https://doc.rust-lang.org/rust-by-example/flow_control/if_else.html
primitive_types2.rs
fn main() {
// Characters (`char`)
// Note the _single_ quotes, these are different from the double quotes
// you've been seeing around.
let my_first_initial = 'C';
if my_first_initial.is_alphabetic() {
println!("Alphabetical!");
} else if my_first_initial.is_numeric() {
println!("Numerical!");
} else {
println!("Neither alphabetic nor numeric!");
}
let your_character = '😻';
// Finish this line like the example! What's your favorite character?
// Try a letter, try a number, try a special character, try a character
// from a different language than your own, try an emoji!
if your_character.is_alphabetic() {
println!("Alphabetical!");
} else if your_character.is_numeric() {
println!("Numerical!");
} else {
println!("Neither alphabetic nor numeric!");
}
}
// Alphabetical!
// Neither alphabetic nor numeric!
your_character
をchar
型で初期化する。char
はシングルクォートで囲む。
参考
- https://doc.rust-lang.org/stable/book/ch03-02-data-types.html#the-character-type
- https://doc.rust-lang.org/rust-by-example/primitives/literals.html
primitive_types3.rs
fn main() {
let a = ['a'; 100];
if a.len() >= 100 {
println!("Wow, that's a big array!");
} else {
println!("Meh, I eat arrays like that for breakfast.");
panic!("Array not big enough, more elements needed")
}
}
// Wow, that's a big array!
配列a
を初期化する。a
は100個以上の配列にする。['a'; 100]
は'a'
を100個持つ配列を初期化する。
参考
- https://doc.rust-lang.org/stable/book/ch03-02-data-types.html#the-array-type
- https://doc.rust-lang.org/rust-by-example/primitives/array.html
primitive_types4.rs
#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];
let nice_slice = &a[1..4];
assert_eq!([2, 3, 4], nice_slice)
}
a
からスライスnice_slice
を作成する。nice_slice
はa
の1番目から4番目までの要素を参照している。
参考
- https://doc.rust-lang.org/stable/book/ch04-03-slices.html#other-slices
- https://doc.rust-lang.org/rust-by-example/primitives/array.html
primitive_types5.rs
fn main() {
let cat = ("Furry McFurson", 3.5);
let (name, age) = cat;
println!("{} is {} years old.", name, age);
}
cat
をname
とage
に分配する。この操作をdestructuring(日本語で非構造化?分配?)という。
参考
- https://doc.rust-lang.org/stable/book/ch03-02-data-types.html#the-tuple-type
- https://doc.rust-lang.org/rust-by-example/primitives/tuples.html
primitive_types6.rs
#[test]
fn indexing_tuple() {
let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax.
let second = numbers.1;
assert_eq!(2, second, "This is not the 2nd number in the tuple!")
}
ドット(.
)でタプルのインデックスにアクセスできる。
参考

Vectors
vecs1.rs
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array
let v = vec![10, 20, 30, 40]; // TODO: declare your vector here with the macro for vectors
(a, v)
}
a
と同様の内容をベクトルで初期化する。vec!
マクロを使って初期化できる。
参考
- https://doc.rust-lang.org/stable/book/ch08-01-vectors.html#creating-a-new-vector
- https://doc.rust-lang.org/rust-by-example/std/vec.html
vecs2.rs
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for element in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is multiplied by 2.
*element *= 2
}
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
v
}
fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter()
.map(|element| {
// TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number!
element * 2
})
.collect()
}
Fill this up so that each element in the Vec
v
is multiplied by 2.
上記にあるように、v
の各要素を2倍する。
vec_loop
ではfor
を使って各要素を2倍する。element
は可変参照なので、*element
で参照外しが必要。
vec_map
ではmap
を使って各要素を2倍する。map
はelement * 2
を返すことで、各要素を2倍した新しいベクトルを作成する。
参考

Move Semantics
move_semantics1.rs
fn main() {
let vec0 = vec![22, 44, 66];
let vec1 = fill_vec(vec0);
assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
vec.push(88);
vec
}
fill_vec
内のvec
はイミュータブルであるので、そのままではpush
できない。そのため、mut
をつけてミュータブルにする。
参考
- https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html#return-values-and-scope
- https://doc.rust-lang.org/rust-by-example/scope/move.html
move_semantics2.rs
fn main() {
let vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(&vec0);
assert_eq!(vec0, vec![22, 44, 66]);
assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(vec: &Vec<i32>) -> Vec<i32> {
let mut vec = vec.clone();
vec.push(88);
vec
}
所有権が移動しないようにfill_vec
にvec0
そのものではなく、&vec0
で参照を渡す。関数の引数に参照を取ることを借用と呼ぶ。
fill_vec
内では所有権を持ったVec<i32>
を返すために、vec.clone()
でコピーを作成する。
参考
- https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#references-and-borrowing
- https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references
- https://doc.rust-lang.org/rust-by-example/scope/borrow.html
move_semantics3.rs
fn main() {
let mut vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec(vec0);
assert_eq!(vec1, vec![22, 44, 66, 88]);
}
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(88);
vec
}
fill_vec
でpush
するために、mut
をつけてvec
をミュータブルにする。
参考
move_semantics4.rs
fn main() {
// let vec0 = vec![22, 44, 66];
let mut vec1 = fill_vec();
assert_eq!(vec1, vec![22, 44, 66, 88]);
}
// `fill_vec()` no longer takes `vec: Vec<i32>` as argument - don't change this!
fn fill_vec() -> Vec<i32> {
// Instead, let's create and fill the Vec in here - how do you do that?
let mut vec = vec![22, 44, 66];
vec.push(88);
vec
}
fill_vec
に引数を渡さないように変更する。新しくvec
をvec!
で初期化してリターンすることで所有権を譲渡する。
参考
move_semantics5.rs
fn main() {
let mut x = 100;
let y = &mut x;
*y += 100;
let z = &mut x;
*z += 1000;
assert_eq!(x, 1200);
}
&mut
可変な参照は同時に複数存在できないので、順番を入れ替える。またy
とz
の型は&mut {integer}
であり{integer}
ではないため、*y
や*z
で参照外しする必要がある。
参考
- https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references
- https://doc.rust-lang.org/rust-by-example/scope/borrow/alias.html
- https://doc.rust-lang.org/book/ch15-02-deref.html#following-the-pointer-to-the-value
move_semantics6.rs
fn main() {
let data = "Rust is great!".to_string();
get_char(&data);
string_uppercase(data);
}
// Should not take ownership
fn get_char(data: &String) -> char {
data.chars().last().unwrap()
}
// Should take ownership
fn string_uppercase(mut data: String) {
data = data.to_uppercase();
println!("{}", data);
}
get_char
は借用するように、string_uppercase
は所有権を譲渡するように変更する。
参考

Structs
structs1.rs
struct ColorClassicStruct {
red: u8,
green: u8,
blue: u8,
}
struct ColorTupleStruct(u8, u8, u8);
#[derive(Debug)]
struct UnitLikeStruct;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
let green = ColorClassicStruct {
red: 0,
green: 255,
blue: 0,
};
assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
assert_eq!(green.blue, 0);
}
#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct!
let green = ColorTupleStruct(0, 255, 0);
assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
assert_eq!(green.2, 0);
}
#[test]
fn unit_structs() {
// TODO: Instantiate a unit-like struct!
let unit_like_struct = UnitLikeStruct;
let message = format!("{:?}s are fun!", unit_like_struct);
assert_eq!(message, "UnitLikeStructs are fun!");
}
}
ColorClassicStruct
でred
, green
, blue
の3つのフィールドを持つ構造体を定義している。
ColorTupleStruct
でタプル構造体を定義している。
UnitLikeStruct
でフィールドの存在しないユニット構造体を定義している。
参考
- https://doc.rust-lang.org/book/ch05-01-defining-structs.html
- https://doc.rust-lang.org/rust-by-example/custom_types/structs.html
structs2.rs
#[derive(Debug)]
struct Order {
name: String,
year: u32,
made_by_phone: bool,
made_by_mobile: bool,
made_by_email: bool,
item_number: u32,
count: u32,
}
fn create_order_template() -> Order {
Order {
name: String::from("Bob"),
year: 2019,
made_by_phone: false,
made_by_mobile: false,
made_by_email: true,
item_number: 123,
count: 0,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
let your_order = Order {
count: 1,
name: String::from("Hacker in Rust"),
..order_template
};
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
assert_eq!(your_order.made_by_mobile, order_template.made_by_mobile);
assert_eq!(your_order.made_by_email, order_template.made_by_email);
assert_eq!(your_order.item_number, order_template.item_number);
assert_eq!(your_order.count, 1);
}
}
..
(update syntax)を使用して、order_template
のフィールドをコピーして、your_order
を作成する。
参考
- https://doc.rust-lang.org/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax
- https://doc.rust-lang.org/rust-by-example/custom_types/structs.html
- https://doc.rust-lang.org/rust-by-example/custom_types/structs.html
structs3.rs
#[derive(Debug)]
struct Package {
sender_country: String,
recipient_country: String,
weight_in_grams: u32,
}
impl Package {
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Package {
if weight_in_grams < 10 {
// This is not how you should handle errors in Rust,
// but we will learn about error handling later.
panic!("Can not ship a package with weight below 10 grams.")
} else {
Package {
sender_country,
recipient_country,
weight_in_grams,
}
}
}
fn is_international(&self) -> bool {
self.sender_country != self.recipient_country
}
fn get_fees(&self, cents_per_gram: u32) -> u32 {
self.weight_in_grams * cents_per_gram
}
}
構造体にはimpl
キーワードを使ってメソッドを追加することができる。メソッドを定義する場合、第1引数として&self
を取得できる。
get_fees(cents_per_gram)
のように渡したパラメータは、fn get_fees(&self, cents_per_gram: u32)
のように第2引数以降として受け取ることができる。
参考

Enums
enums1.rs
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor,
}
fn main() {
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Move);
println!("{:?}", Message::ChangeColor);
}
enum
キーワードで列挙型を定義する。
参考
- https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html#defining-an-enum
- https://doc.rust-lang.org/rust-by-example/custom_types/enum.html
enums2.rs
enum Message {
// TODO: define the different variants used below
Move { x: u32, y: u32 },
Echo(String),
ChangeColor(u8, u8, u8),
Quit,
}
impl Message {
fn call(&self) {
println!("{:?}", self);
}
}
fn main() {
let messages = [
Message::Move { x: 10, y: 30 },
Message::Echo(String::from("hello world")),
Message::ChangeColor(200, 255, 255),
Message::Quit,
];
for message in &messages {
message.call();
}
}
main
内のmessages
と矛盾しないように、enum Message
の定義を修正する。
参考
- https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html#enum-values
- https://doc.rust-lang.org/rust-by-example/custom_types/enum.html#type-aliases
enums3.rs
enum Message {
// TODO: implement the message variant types based on their usage below
Move(Point),
Echo(String),
ChangeColor(u8, u8, u8),
Quit,
}
struct Point {
x: u8,
y: u8,
}
struct State {
color: (u8, u8, u8),
position: Point,
quit: bool,
message: String,
}
impl State {
fn change_color(&mut self, color: (u8, u8, u8)) {
self.color = color;
}
fn quit(&mut self) {
self.quit = true;
}
fn echo(&mut self, s: String) {
self.message = s
}
fn move_position(&mut self, p: Point) {
self.position = p;
}
fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
// Remember: When passing a tuple as a function argument, you'll need extra parentheses:
// fn function((t, u, p, l, e))
match message {
Message::Move(p) => self.move_position(p),
Message::ChangeColor(r, g, b) => self.change_color((r, g, b)),
Message::Echo(s) => self.echo(s),
Message::Quit => self.quit(),
}
}
}
process
メソッドでテストが通るようにmatch
式を実装する。
参考

Strings
strings1.rs
fn main() {
let answer = current_favorite_color();
println!("My current favorite color is {}", answer);
}
fn current_favorite_color() -> String {
// "blue".to_string()
String::from("blue")
}
関数の定義や型は変更せずにString
を返すように修正する。"blue".to_string()
でも良い。
参考
- https://doc.rust-lang.org/book/ch08-02-strings.html#creating-a-new-string
- https://doc.rust-lang.org/rust-by-example/std/str.html#strings
strings2.rs
fn main() {
let word = String::from("green"); // Try not changing this line :)
if is_a_color_word(word) {
println!("That is a color word I know!");
} else {
println!("That is not a color word I know.");
}
}
fn is_a_color_word(attempt: String) -> bool {
attempt == "green" || attempt == "blue" || attempt == "red"
}
String
および&str
は違う型なので、String
を引数に取るように修正する。
- https://doc.rust-lang.org/book/ch08-02-strings.html#what-is-a-string
- https://doc.rust-lang.org/rust-by-example/std/str.html#strings
strings3.rs
fn trim_me(input: &str) -> String {
// TODO: Remove whitespace from both ends of a string!
input.trim().to_string()
}
fn compose_me(input: &str) -> String {
// TODO: Add " world!" to the string! There's multiple ways to do this!
format!("{input} world!")
}
fn replace_me(input: &str) -> String {
// TODO: Replace "cars" in the string with "balloons"!
input.replace("cars", "balloons")
}
trim_me
とreplace_me
は標準ライブラリのメソッドを使って修正する。compose_me
はformat!
マクロを使って修正する。
参考
- https://doc.rust-lang.org/std/string/struct.String.html#method.trim
- https://doc.rust-lang.org/std/string/struct.String.html#method.replace
- https://doc.rust-lang.org/book/ch08-02-strings.html#concatenation-with-the--operator-or-the-format-macro
strings4.rs
fn string_slice(arg: &str) {
println!("{}", arg);
}
fn string(arg: String) {
println!("{}", arg);
}
fn main() {
string_slice("blue");
string("red".to_string());
string(String::from("hi"));
string("rust is fun!".to_owned());
string("nice weather".into());
string(format!("Interpolation {}", "Station"));
string_slice(&String::from("abc")[0..1]);
string_slice(" hello there ".trim());
string("Happy Monday!".to_string().replace("Mon", "Tues"));
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
}
String
型か&str
型かを判断して適切な関数を呼び出すように修正する。
参考

Modules
modules1.rs
mod sausage_factory {
// Don't let anybody outside of this module see this!
fn get_secret_recipe() -> String {
String::from("Ginger")
}
pub fn make_sausage() {
get_secret_recipe();
println!("sausage!");
}
}
fn main() {
sausage_factory::make_sausage();
}
pub
キーワードでmake_sausage
を公開する。
参考
- https://doc.rust-lang.org/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html#exposing-paths-with-the-pub-keyword
- https://doc.rust-lang.org/rust-by-example/mod/visibility.html
modules2.rs
mod delicious_snacks {
// TODO: Fix these use statements
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
mod fruits {
pub const PEAR: &'static str = "Pear";
pub const APPLE: &'static str = "Apple";
}
mod veggies {
pub const CUCUMBER: &'static str = "Cucumber";
pub const CARROT: &'static str = "Carrot";
}
}
fn main() {
println!(
"favorite snacks: {} and {}",
delicious_snacks::fruit,
delicious_snacks::veggie
);
}
// favorite snacks: Pear and Cucumber
use self::fruits::PEAR
およびuse self::veggies::CUCUMBER
をpub
で公開して、as
で別名をつける。
参考
- https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#creating-idiomatic-use-paths
- https://doc.rust-lang.org/rust-by-example/mod/use.html
modules3.rs
// use std::time::SystemTime;
// use std::time::UNIX_EPOCH;
// ↑の2行を1行にまとめる
use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()),
Err(_) => panic!("SystemTime before UNIX EPOCH!"),
}
}
// 1970-01-01 00:00:00 UTC was 1707893223 seconds ago!
SystemTime
とUNIX_EPOCH
をuse
でstd::time
から取り込む。std::time::{SystemTime, UNIX_EPOCH};
はstd::time::SystemTime
とstd::time::UNIX_EPOCH
を取り込む。
参考

Hashmaps
hashmaps1.rs
use std::collections::HashMap;
fn fruit_basket() -> HashMap<String, u32> {
let mut basket = HashMap::new();
// Two bananas are already given for you :)
basket.insert(String::from("banana"), 2);
// TODO: Put more fruits in your basket here.
basket.insert(String::from("apple"), 2);
basket.insert(String::from("orange"), 2);
basket
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn at_least_three_types_of_fruits() {
let basket = fruit_basket();
assert!(basket.len() >= 3);
}
#[test]
fn at_least_five_fruits() {
let basket = fruit_basket();
assert!(basket.values().sum::<u32>() >= 5);
}
}
HashMap::new()
でbasket
を初期化する。テスト条件から、basket
には3種類以上の果物が入っていること、果物の総数が5以上であることが求められている。
参考
- https://doc.rust-lang.org/book/ch08-03-hash-maps.html#creating-a-new-hash-map
- https://doc.rust-lang.org/rust-by-example/std/hash.html
hashmaps2.rs
enum Fruit {
Apple,
Banana,
Mango,
Lychee,
Pineapple,
}
fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
let fruit_kinds = vec![
Fruit::Apple,
Fruit::Banana,
Fruit::Mango,
Fruit::Lychee,
Fruit::Pineapple,
];
for fruit in fruit_kinds {
// TODO: Insert new fruits if they are not already present in the
// basket. Note that you are not allowed to put any type of fruit that's
// already present!
basket.entry(fruit).or_insert(1);
}
}
fn fruit_basket
関数において、basket
にfruit_kinds
のキーがなければ1
を挿入する。
参考
hashmaps3.rs
use std::collections::HashMap;
// A structure to store the goal details of a team.
struct Team {
goals_scored: u8,
goals_conceded: u8,
}
fn build_scores_table(results: String) -> HashMap<String, Team> {
// The name of the team is the key and its associated struct is the value.
let mut scores: HashMap<String, Team> = HashMap::new();
for r in results.lines() {
let v: Vec<&str> = r.split(',').collect();
let team_1_name = v[0].to_string();
let team_1_score: u8 = v[2].parse().unwrap();
let team_2_name = v[1].to_string();
let team_2_score: u8 = v[3].parse().unwrap();
// TODO: Populate the scores table with details extracted from the
// current line. Keep in mind that goals scored by team_1
// will be the number of goals conceded by team_2, and similarly
// goals scored by team_2 will be the number of goals conceded by
// team_1.
let team_1 = scores.entry(team_1_name).or_insert(Team {
goals_scored: 0,
goals_conceded: 0,
});
team_1.goals_scored += team_1_score;
team_1.goals_conceded += team_2_score;
let team_2 = scores.entry(team_2_name).or_insert(Team {
goals_scored: 0,
goals_conceded: 0,
});
team_2.goals_scored += team_2_score;
team_2.goals_conceded += team_1_score;
}
scores
}
r
には"England,France,4,2"
のような文字列が入ってくるので、それぞれをteam_1_name
、team_2_name
、team_1_score
、team_2_score
に分割する。
scores
はチーム名をキーとして、Team
(goals_scored
得点とgoals_scored
失点からなる)を値とするハッシュマップである。or_insert
はキーに対する値への参照を返すので、それに対してgoals_scored
とgoals_conceded
を更新する。
参考

quiz2
pub enum Command {
Uppercase,
Trim,
Append(usize),
}
mod my_module {
use super::Command;
// TODO: Complete the function signature!
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
// TODO: Complete the output declaration!
let mut output: Vec<String> = vec![];
for (string, command) in input.iter() {
// TODO: Complete the function body. You can do it!
let result = match command {
Command::Uppercase => string.to_uppercase(),
Command::Trim => string.trim().to_string(),
Command::Append(n) => format!("{}{}", string, "bar".repeat(*n)),
};
output.push(result);
}
output
}
}
#[cfg(test)]
mod tests {
// TODO: What do we need to import to have `transformer` in scope?
use super::my_module::transformer;
use super::Command;
#[test]
fn it_works() {
let output = transformer(vec![
("hello".into(), Command::Uppercase),
(" all roads lead to rome! ".into(), Command::Trim),
("foo".into(), Command::Append(1)),
("bar".into(), Command::Append(5)),
]);
assert_eq!(output[0], "HELLO");
assert_eq!(output[1], "all roads lead to rome!");
assert_eq!(output[2], "foobar");
assert_eq!(output[3], "barbarbarbarbarbar");
}
}
// - Uppercase the string
// - Trim the string
// - Append "bar" to the string a specified amount of times
上記の条件を見ながらコマンドに合わせてtransformer
のロジックを記述する。
Command::Append(n) => format!("{}{}", string, "bar".repeat(*n))
で*n
となる理由は、iter()
が参照のイテレータを生成するため。もし所有権を取得したい場合はinto_iter()
を使用する。

Options
options1.rs
fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a
// value of 0 The Option output should gracefully handle cases where
// time_of_day > 23.
// TODO: Complete the function body - remember to return an Option!
match time_of_day {
0..=21 => Some(5),
22..=24 => Some(0),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_icecream() {
assert_eq!(maybe_icecream(9), Some(5));
assert_eq!(maybe_icecream(10), Some(5));
assert_eq!(maybe_icecream(23), Some(0));
assert_eq!(maybe_icecream(22), Some(0));
assert_eq!(maybe_icecream(25), None);
}
#[test]
fn raw_value() {
// TODO: Fix this test. How do you get at the value contained in the
// Option?
let icecreams = maybe_icecream(12);
assert_eq!(icecreams, Some(5));
}
}
maybe_icecream
関数内で、match
式を使ってOption enum
を返す。またOption<T>
の比較はOption<T>
と行う必要がある。
参考
- https://doc.rust-lang.org/stable/book/ch06-01-defining-an-enum.html#the-option-enum-and-its-advantages-over-null-values
- https://doc.rust-lang.org/stable/book/ch06-02-match.html#matching-with-optiont
- https://doc.rust-lang.org/stable/book/ch18-03-pattern-syntax.html
options2.rs
mod tests {
#[test]
fn simple_option() {
let target = "rustlings";
let optional_target = Some(target);
// TODO: Make this an if let statement whose value is "Some" type
if let Some(word) = optional_target {
assert_eq!(word, target);
}
}
#[test]
fn layered_option() {
let range = 10;
let mut optional_integers: Vec<Option<i8>> = vec![None];
for i in 1..(range + 1) {
optional_integers.push(Some(i));
}
let mut cursor = range;
// TODO: make this a while let statement - remember that vector.pop also
// adds another layer of Option<T>. You can stack `Option<T>`s into while let and if let.
while let Some(integer) = optional_integers.pop() {
if let Some(integer) = integer {
assert_eq!(integer, cursor);
cursor -= 1;
}
}
assert_eq!(cursor, 0);
}
}
simple_option
はif let
式を使ってoptional_target
がSome<T>
の時に<T>
を取り出しつつ、処理が走るようにする。
layered_option
はwhile let
式を使う。pop()
はOption<T>
を返すので、戻り値がOption<Option<T>>
になっていることに注意する。while let
とif let
を使えとあるので、while let
の中でif let
を使ってinteger
を取り出す。
参考
- https://doc.rust-lang.org/stable/book/ch06-03-if-let.html
- https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html
options3.rs
struct Point {
x: i32,
y: i32,
}
fn main() {
let y: Option<Point> = Some(Point { x: 100, y: 200 });
match y {
Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y),
_ => panic!("no match!"),
}
y; // Fix without deleting this line.
}
Some(ref p)
のref
は参照を生成する。これにより、y
の所有権を奪わずに済むので、y
をその後も使うことができる。
参考

Error handling
errors1.rs
pub fn generate_nametag_text(name: String) -> Result<String, String> {
if name.is_empty() {
// Empty names aren't allowed.
Err("`name` was empty; it must be nonempty.".into())
} else {
Ok(format!("Hi! My name is {}", name))
}
}
Option<T>
ではなくResult<T, E>
を返すように修正。
参考
- https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html
- https://doc.rust-lang.org/rust-by-example/error/result.html
errors2.rs
use std::num::ParseIntError;
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>();
match qty {
Ok(q) => Ok(q * cost_per_item + processing_fee),
Err(e) => Err(e),
}
}
str::parse
をはResult
を返すので、match
でOk
とErr
の場合の処理を書く。
もしくは次のように?
演算子を使っても良い。
use std::num::ParseIntError;
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>()?;
Ok(qty * cost_per_item + processing_fee)
}
参考
- https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
- https://doc.rust-lang.org/rust-by-example/error/result.html
- https://doc.rust-lang.org/std/primitive.str.html#method.parse
errors3.rs
use std::num::ParseIntError;
fn main() -> Result<(), ParseIntError> {
let mut tokens = 100;
let pretend_user_input = "8";
let cost = total_cost(pretend_user_input)?;
if cost > tokens {
println!("You can't afford that many!");
} else {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}
Ok(())
}
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>()?;
Ok(qty * cost_per_item + processing_fee)
}
main
で?
を使うために、main
からResult
を返すように修正。成功した場合はOk(())
を返す。
参考
- https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#where-the--operator-can-be-used
- https://doc.rust-lang.org/rust-by-example/error/result.html#using-result-in-main
errors4.rs
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
// Hmm... Why is this always returning an Ok value?
match value {
v if v > 0 => Ok(PositiveNonzeroInteger(v as u64)),
v if v == 0 => Err(CreationError::Zero),
_ => Err(CreationError::Negative),
}
}
}
PositiveNonzeroInteger::new
が条件によってOk
とErr
を返すように修正。
参考
- https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html
- https://doc.rust-lang.org/rust-by-example/error/result.html
errors5.rs
use std::error;
use std::fmt;
use std::num::ParseIntError;
// TODO: update the return type of `main()` to make this compile.
fn main() -> Result<(), Box<dyn error::Error>> {
let pretend_user_input = "42";
let x: i64 = pretend_user_input.parse()?;
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
Ok(())
}
Box<dyn error::Error> to mean “any kind of error.”
ここではBox<dyn error::Error>
が「あらゆる種類のエラー」を意味することがわかってればいい。詳しくはTrait
の部分で確認する。
parse()
とPositiveNonzeroInteger::new(x)
で発生する可能性がある異なるエラーをBox<dyn error::Error>
で表現している。
参考
- https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
- https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/boxing_errors.html
errors6.rs
use std::num::ParseIntError;
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
#[derive(PartialEq, Debug)]
enum ParsePosNonzeroError {
Creation(CreationError),
ParseInt(ParseIntError),
}
impl ParsePosNonzeroError {
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
ParsePosNonzeroError::Creation(err)
}
// TODO: add another error conversion function here.
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
ParsePosNonzeroError::ParseInt(err)
}
}
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
// TODO: change this to return an appropriate error instead of panicking
// when `parse()` returns an error.
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
Ok(PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)?)
}
ParsePosNonzeroError
にParseIntError
を変換する関数を追加。
map_err()
を使うことでOk
を変えずに、任意のErr
に変換できる。
参考

Generics
generics1.rs
fn main() {
let mut shopping_list: Vec<&str> = Vec::new();
shopping_list.push("milk");
}
ベクターはVec<T>
であり、これはジェネリック型である。宣言時に<T>
の部分に任意の型を指定することができる。
参考
- https://doc.rust-lang.org/book/ch10-01-syntax.html
- https://doc.rust-lang.org/rust-by-example/generics.html
generics2.rs
struct Wrapper<T> {
value: T,
}
impl<T> Wrapper<T> {
pub fn new(value: T) -> Self {
Wrapper { value }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn store_u32_in_wrapper() {
assert_eq!(Wrapper::new(42).value, 42);
}
#[test]
fn store_str_in_wrapper() {
assert_eq!(Wrapper::new("Foo").value, "Foo");
}
}
構造体およびメソッドでジェネリクスを定義する。
参考

Traits
traits1.rs
trait AppendBar {
fn append_bar(self) -> Self;
}
impl AppendBar for String {
// TODO: Implement `AppendBar` for type `String`.
fn append_bar(self) -> Self {
format!("{}Bar", self)
}
}
fn main() {
let s = String::from("Foo");
let s = s.append_bar();
println!("s: {}", s);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_foo_bar() {
assert_eq!(String::from("Foo").append_bar(), String::from("FooBar"));
}
#[test]
fn is_bar_bar() {
assert_eq!(
String::from("").append_bar().append_bar(),
String::from("BarBar")
);
}
}
trait AppendBar
で定義されているメソッドをString
型に対して実装する。append_bar
は自身を受け取り、Bar
を追加した文字列を返す。
参考
- https://doc.rust-lang.org/book/ch10-02-traits.html#implementing-a-trait-on-a-type
- https://doc.rust-lang.org/rust-by-example/trait.html
traits2.rs
trait AppendBar {
fn append_bar(self) -> Self;
}
// TODO: Implement trait `AppendBar` for a vector of strings.
impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Self {
self.push(String::from("Bar"));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_vec_pop_eq_bar() {
let mut foo = vec![String::from("Foo")].append_bar();
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
assert_eq!(foo.pop().unwrap(), String::from("Foo"));
}
}
Vec<String>
に対して、AppendBar
トレイトを実装する。append_bar
は自身を受け取り、Bar
を追加したVec
を返す。
参考
- https://doc.rust-lang.org/book/ch10-02-traits.html#implementing-a-trait-on-a-type
- https://doc.rust-lang.org/rust-by-example/trait.html
traits3.rs
pub trait Licensed {
fn licensing_info(&self) -> String {
String::from("Some information")
}
}
struct SomeSoftware {
version_number: i32,
}
struct OtherSoftware {
version_number: String,
}
impl Licensed for SomeSoftware {} // Don't edit this line
impl Licensed for OtherSoftware {} // Don't edit this line
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_licensing_info_the_same() {
let licensing_info = String::from("Some information");
let some_software = SomeSoftware { version_number: 1 };
let other_software = OtherSoftware {
version_number: "v2.0.0".to_string(),
};
assert_eq!(some_software.licensing_info(), licensing_info);
assert_eq!(other_software.licensing_info(), licensing_info);
}
}
Licensed
トレイトにlicensing_info
の型だけでなくデフォルトの振る舞いを実装する。
参考
- https://doc.rust-lang.org/book/ch10-02-traits.html#default-implementations
- https://doc.rust-lang.org/rust-by-example/trait.html
traits4.rs
pub trait Licensed {
fn licensing_info(&self) -> String {
"some information".to_string()
}
}
struct SomeSoftware {}
struct OtherSoftware {}
impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}
// YOU MAY ONLY CHANGE THE NEXT LINE
fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool {
software.licensing_info() == software_two.licensing_info()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compare_license_information() {
let some_software = SomeSoftware {};
let other_software = OtherSoftware {};
assert!(compare_license_types(some_software, other_software));
}
#[test]
fn compare_license_information_backwards() {
let some_software = SomeSoftware {};
let other_software = OtherSoftware {};
assert!(compare_license_types(other_software, some_software));
}
}
compare_license_types(software: impl Licensed, software_two: impl Licensed)
のようにすることで、Licensed
トレイトを実装した型を引数に取ることができる。
参考
traits5.rs
pub trait SomeTrait {
fn some_function(&self) -> bool {
true
}
}
pub trait OtherTrait {
fn other_function(&self) -> bool {
true
}
}
struct SomeStruct {}
struct OtherStruct {}
impl SomeTrait for SomeStruct {}
impl OtherTrait for SomeStruct {}
impl SomeTrait for OtherStruct {}
impl OtherTrait for OtherStruct {}
// YOU MAY ONLY CHANGE THE NEXT LINE
fn some_func(item: (impl SomeTrait + OtherTrait)) -> bool {
// fn some_func<T: SomeTrait + OtherTrait>(item: T) -> bool
item.some_function() && item.other_function()
}
fn main() {
some_func(SomeStruct {});
some_func(OtherStruct {});
}
some_func(item: (impl SomeTrait + OtherTrait))
のようにすることで、SomeTrait
とOtherTrait
を実装した型を引数に取ることができる。
もしくはトレイト境界を使ってfn some_func<T: SomeTrait + OtherTrait>(item: T)
のようにすることもできる。
参考

quiz3
use std::fmt::Display;
pub struct ReportCard<T> {
pub grade: T,
pub student_name: String,
pub student_age: u8,
}
impl<T: Display> ReportCard<T> {
fn print(&self) -> String {
format!(
"{} ({}) - achieved a grade of {}",
&self.student_name, &self.student_age, &self.grade
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn generate_numeric_report_card() {
let report_card = ReportCard {
grade: 2.1,
student_name: "Tom Wriggle".to_string(),
student_age: 12,
};
assert_eq!(
report_card.print(),
"Tom Wriggle (12) - achieved a grade of 2.1"
);
}
#[test]
fn generate_alphabetic_report_card() {
// TODO: Make sure to change the grade here after you finish the exercise.
let report_card = ReportCard {
grade: "A+",
student_name: "Gary Plotter".to_string(),
student_age: 11,
};
assert_eq!(
report_card.print(),
"Gary Plotter (11) - achieved a grade of A+"
);
}
}
format!("{}", x);
で{}
を使用する時、x
にはDisplay
トレイトが実装されていることが求められる。
ReportCard
構造体のgrade
フィールドでジェネリクスを使いたいが、 fn print
メソッドを見るとgrade
はformat!
の{}
で表示する必要がある。つまりジェネリクスを使いつつ、Display
を保証する必要がある。
impl<T: Display> ReportCard<T>
とすることでDisplay
を保証したジェネリクスを表現できる。
参考

Lifetimes
lifetimes1.rs
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
fn main() {
let string1 = String::from("abcd");
let string2 = "xyz";
let result = longest(string1.as_str(), string2);
println!("The longest string is '{}'", result);
}
// The longest string is 'abcd'
'a
でライフタイムを指定する。x
とy
のライフタイムが同じである(短い方になる)ことを示している。
参考
- https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-annotations-in-function-signatures
- https://doc.rust-lang.org/stable/rust-by-example/scope/lifetime/explicit.html
lifetimes2.rs
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
fn main() {
let string1 = String::from("long string is long");
let string2 = String::from("xyz");
let result;
{
result = longest(string1.as_str(), string2.as_str());
}
println!("The longest string is '{}'", result);
}
もしくは次でもOK。
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
fn main() {
let string1 = String::from("long string is long");
let result;
{
let string2 = String::from("xyz");
result = longest(string1.as_str(), string2.as_str());
println!("The longest string is '{}'", result);
}
}
longest
関数はstring1
とstring2
のライフタイムが同じであることを示している。string2
のライフタイムをstring1
と同じになるように修正する。
もしくはprintln!
をstring2
のライフタイムと同じスコープにする。
参考
- https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-annotations-in-function-signatures
- https://doc.rust-lang.org/stable/rust-by-example/scope/lifetime/explicit.html
lifetimes3.rs
struct Book<'a> {
author: &'a str,
title: &'a str,
}
fn main() {
let name = String::from("Jill Smith");
let title = String::from("Fish Flying");
let book = Book {
author: &name,
title: &title,
};
println!("{} by {}", book.title, book.author);
}
// Fish Flying by Jill Smith
Book
構造体のフィールドは&str
型であり、参照なのでライフタイムを指定する必要がある。
参考

Tests
tests1.rs
#[cfg(test)]
mod tests {
#[test]
fn you_can_assert() {
assert!(1 == 1);
}
}
assert!
マクロの中がtrue
であればテストが通る。
参考
- https://doc.rust-lang.org/book/ch11-01-writing-tests.html#checking-results-with-the-assert-macro
- https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html
tests2.rs
#[cfg(test)]
mod tests {
#[test]
fn you_can_assert_eq() {
assert_eq!(1 + 1, 2);
}
}
assert_eq!(x, y)
マクロのx
とy
が等しければテストが通る。
参考
- https://doc.rust-lang.org/book/ch11-01-writing-tests.html#testing-equality-with-the-assert_eq-and-assert_ne-macros
- https://doc.rust-lang.org/stable/rust-by-example/testing/unit_testing.html
tests3.rs
pub fn is_even(num: i32) -> bool {
num % 2 == 0
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_true_when_even() {
assert!(is_even(4));
}
#[test]
fn is_false_when_odd() {
assert!(!is_even(5));
}
}
!is_even(5)
の時は否定を書く。
参考
tests4.rs
struct Rectangle {
width: i32,
height: i32,
}
impl Rectangle {
// Only change the test functions themselves
pub fn new(width: i32, height: i32) -> Self {
if width <= 0 || height <= 0 {
panic!("Rectangle width and height cannot be negative!")
}
Rectangle { width, height }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn correct_width_and_height() {
// This test should check if the rectangle is the size that we pass into its constructor
let rect = Rectangle::new(10, 20);
assert_eq!(rect.width, 10); // check width
assert_eq!(rect.height, 20); // check height
}
#[test]
#[should_panic]
fn negative_width() {
// This test should check if program panics when we try to create rectangle with negative width
let _rect = Rectangle::new(-10, 10);
}
#[test]
#[should_panic]
fn negative_height() {
// This test should check if program panics when we try to create rectangle with negative height
let _rect = Rectangle::new(10, -10);
}
}
#[should_panic]
属性を使って、テストがパニックすることを確認する。
参考

Iterators
iterators1.rs
#[test]
fn main() {
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
}
iter()
でイテレータを作成する。next()
はOption
を返す。次の要素がある時はSome
で、ない時はNone
を返す。
参考
iterators2.rs
// Step 1.
// Complete the `capitalize_first` function.
// "hello" -> "Hello"
pub fn capitalize_first(input: &str) -> String {
let mut c = input.chars();
match c.next() {
None => String::new(),
Some(first) => first.to_uppercase().to_string() + c.as_str(),
}
}
// Step 2.
// Apply the `capitalize_first` function to a slice of string slices.
// Return a vector of strings.
// ["hello", "world"] -> ["Hello", "World"]
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
words
.iter()
.map(|v| capitalize_first(v))
.collect::<Vec<String>>()
}
// Step 3.
// Apply the `capitalize_first` function again to a slice of string slices.
// Return a single string.
// ["hello", " ", "world"] -> "Hello World"
pub fn capitalize_words_string(words: &[&str]) -> String {
capitalize_words_vector(words).concat()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_success() {
assert_eq!(capitalize_first("hello"), "Hello");
}
#[test]
fn test_empty() {
assert_eq!(capitalize_first(""), "");
}
#[test]
fn test_iterate_string_vec() {
let words = vec!["hello", "world"];
assert_eq!(capitalize_words_vector(&words), ["Hello", "World"]);
}
#[test]
fn test_iterate_into_string() {
let words = vec!["hello", " ", "world"];
assert_eq!(capitalize_words_string(&words), "Hello World");
}
}
let mut c = input.chars();
でmut
にすることを忘れない。next()
はイテレータを消費し、内部の状態を変更する。
イテレータを消費するためにmap
した後にcollect
することが必要。
concat()
はVec<String>
をString
に結合する。
参考
iterators3.rs
#[derive(Debug, PartialEq, Eq)]
pub enum DivisionError {
NotDivisible(NotDivisibleError),
DivideByZero,
}
#[derive(Debug, PartialEq, Eq)]
pub struct NotDivisibleError {
dividend: i32,
divisor: i32,
}
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
// Otherwise, return a suitable error.
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
if b == 0 {
Err(DivisionError::DivideByZero)
} else if a % b != 0 {
Err(DivisionError::NotDivisible(NotDivisibleError {
dividend: a,
divisor: b,
}))
} else {
Ok(a / b)
}
}
// Complete the function and return a value of the correct type so the test
// passes.
// Desired output: Ok([1, 11, 1426, 3])
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
division_results.collect()
}
// Complete the function and return a value of the correct type so the test
// passes.
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
let numbers = vec![27, 297, 38502, 81];
let division_results = numbers.into_iter().map(|n| divide(n, 27));
division_results.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_success() {
assert_eq!(divide(81, 9), Ok(9));
}
#[test]
fn test_not_divisible() {
assert_eq!(
divide(81, 6),
Err(DivisionError::NotDivisible(NotDivisibleError {
dividend: 81,
divisor: 6
}))
);
}
#[test]
fn test_divide_by_0() {
assert_eq!(divide(81, 0), Err(DivisionError::DivideByZero));
}
#[test]
fn test_divide_0_by_something() {
assert_eq!(divide(0, 81), Ok(0));
}
#[test]
fn test_result_with_list() {
assert_eq!(format!("{:?}", result_with_list()), "Ok([1, 11, 1426, 3])");
}
#[test]
fn test_list_of_results() {
assert_eq!(
format!("{:?}", list_of_results()),
"[Ok(1), Ok(11), Ok(1426), Ok(3)]"
);
}
}
divide()
関数でResult
の出し分けを行う。collect()
関数で期待する型を明示することで、柔軟な変換が可能。
参考
- https://doc.rust-lang.org/book/ch13-02-iterators.html
- https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect
iterators4.rs
pub fn factorial(num: u64) -> u64 {
// Complete this function to return the factorial of num
// Do not use:
// - return
// Try not to use:
// - imperative style loops (for, while)
// - additional variables
// For an extra challenge, don't use:
// - recursion
// Execute `rustlings hint iterators4` for hints.
(1..=num).fold(1, |acc, x| acc * x)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn factorial_of_0() {
assert_eq!(1, factorial(0));
}
#[test]
fn factorial_of_1() {
assert_eq!(1, factorial(1));
}
#[test]
fn factorial_of_2() {
assert_eq!(2, factorial(2));
}
#[test]
fn factorial_of_4() {
assert_eq!(24, factorial(4));
}
}
fold()
メソッドを使って、累乗を計算する。
参考
- https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.fold
- https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html?highlight=range#matching-ranges-of-values-with-
iterators5.rs
use std::collections::HashMap;
#[derive(Clone, Copy, PartialEq, Eq)]
enum Progress {
None,
Some,
Complete,
}
fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
let mut count = 0;
for val in map.values() {
if val == &value {
count += 1;
}
}
count
}
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
// map is a hashmap with String keys and Progress values.
// map = { "variables1": Complete, "from_str": None, ... }
map.iter().filter(|&(k, v)| *v == value).count()
}
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
let mut count = 0;
for map in collection {
for val in map.values() {
if val == &value {
count += 1;
}
}
}
count
}
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
// collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ]
collection
.iter()
.flat_map(|map| map.values())
.filter(|&v| *v == value)
.count()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn count_complete() {
let map = get_map();
assert_eq!(3, count_iterator(&map, Progress::Complete));
}
#[test]
fn count_some() {
let map = get_map();
assert_eq!(1, count_iterator(&map, Progress::Some));
}
#[test]
fn count_none() {
let map = get_map();
assert_eq!(2, count_iterator(&map, Progress::None));
}
#[test]
fn count_complete_equals_for() {
let map = get_map();
let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
for progress_state in progress_states {
assert_eq!(
count_for(&map, progress_state),
count_iterator(&map, progress_state)
);
}
}
#[test]
fn count_collection_complete() {
let collection = get_vec_map();
assert_eq!(
6,
count_collection_iterator(&collection, Progress::Complete)
);
}
#[test]
fn count_collection_some() {
let collection = get_vec_map();
assert_eq!(1, count_collection_iterator(&collection, Progress::Some));
}
#[test]
fn count_collection_none() {
let collection = get_vec_map();
assert_eq!(4, count_collection_iterator(&collection, Progress::None));
}
#[test]
fn count_collection_equals_for() {
let progress_states = vec![Progress::Complete, Progress::Some, Progress::None];
let collection = get_vec_map();
for progress_state in progress_states {
assert_eq!(
count_collection_for(&collection, progress_state),
count_collection_iterator(&collection, progress_state)
);
}
}
fn get_map() -> HashMap<String, Progress> {
use Progress::*;
let mut map = HashMap::new();
map.insert(String::from("variables1"), Complete);
map.insert(String::from("functions1"), Complete);
map.insert(String::from("hashmap1"), Complete);
map.insert(String::from("arc1"), Some);
map.insert(String::from("as_ref_mut"), None);
map.insert(String::from("from_str"), None);
map
}
fn get_vec_map() -> Vec<HashMap<String, Progress>> {
use Progress::*;
let map = get_map();
let mut other = HashMap::new();
other.insert(String::from("variables2"), Complete);
other.insert(String::from("functions2"), Complete);
other.insert(String::from("if1"), Complete);
other.insert(String::from("from_into"), None);
other.insert(String::from("try_from_into"), None);
vec![map, other]
}
}
count_collection_for
とcount_iterator
を修正する。
iter().flat_map(|map| map.values())
でHashMap
のvalues
のイテレータを、1つのフラットなイテレータに結合する。
参考

Smart Pointers
arc1.rs
#![forbid(unused_imports)] // Do not change this, (or the next) line.
use std::sync::Arc;
use std::thread;
fn main() {
let numbers: Vec<_> = (0..100u32).collect();
let shared_numbers = Arc::new(numbers);
let mut joinhandles = Vec::new();
for offset in 0..8 {
let child_numbers = Arc::clone(&shared_numbers);
joinhandles.push(thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|&&n| n % 8 == offset).sum();
println!("Sum of offset {} is {}", offset, sum);
}));
}
for handle in joinhandles.into_iter() {
handle.join().unwrap();
}
}
thread::spawn
でスレッドを生成し、handle.join()
でスレッドの終了を待っている。メインスレッドで定義した変数をthread::spawn
の中で使用するためにmove
する必要がある。
Arc
を使うことで複数のスレッド間でデータを共有することができる。
参考
- https://doc.rust-lang.org/stable/book/ch16-01-threads.html
- https://doc.rust-lang.org/stable/book/ch16-03-shared-state.html#atomic-reference-counting-with-arct
box1.rs
#[derive(PartialEq, Debug)]
pub enum List {
Cons(i32, Box<List>),
Nil,
}
fn main() {
println!("This is an empty cons list: {:?}", create_empty_list());
println!(
"This is a non-empty cons list: {:?}",
create_non_empty_list()
);
}
pub fn create_empty_list() -> List {
List::Nil
}
pub fn create_non_empty_list() -> List {
List::Cons(1, Box::new(List::Cons(2, Box::new(List::Nil))))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_empty_list() {
assert_eq!(List::Nil, create_empty_list())
}
#[test]
fn test_create_non_empty_list() {
assert_ne!(create_empty_list(), create_non_empty_list())
}
}
再帰的な型はコンパイル時にサイズがわからない。Box<T>
を使うことでヒープにデータを格納し、値にそのポインタを格納することができるので、再帰的な型を定義することができる。
参考
rc1.rs
use std::rc::Rc;
#[derive(Debug)]
struct Sun {}
#[derive(Debug)]
enum Planet {
Mercury(Rc<Sun>),
Venus(Rc<Sun>),
Earth(Rc<Sun>),
Mars(Rc<Sun>),
Jupiter(Rc<Sun>),
Saturn(Rc<Sun>),
Uranus(Rc<Sun>),
Neptune(Rc<Sun>),
}
impl Planet {
fn details(&self) {
println!("Hi from {:?}!", self)
}
}
#[test]
fn main() {
let sun = Rc::new(Sun {});
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
let mercury = Planet::Mercury(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
mercury.details();
let venus = Planet::Venus(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
venus.details();
let earth = Planet::Earth(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
earth.details();
let mars = Planet::Mars(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
mars.details();
let jupiter = Planet::Jupiter(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
jupiter.details();
// TODO
let saturn = Planet::Saturn(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
saturn.details();
// TODO
let uranus = Planet::Uranus(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
uranus.details();
// TODO
let neptune = Planet::Neptune(Rc::clone(&sun));
println!("reference count = {}", Rc::strong_count(&sun)); // 9 references
neptune.details();
assert_eq!(Rc::strong_count(&sun), 9);
drop(neptune);
println!("reference count = {}", Rc::strong_count(&sun)); // 8 references
drop(uranus);
println!("reference count = {}", Rc::strong_count(&sun)); // 7 references
drop(saturn);
println!("reference count = {}", Rc::strong_count(&sun)); // 6 references
drop(jupiter);
println!("reference count = {}", Rc::strong_count(&sun)); // 5 references
drop(mars);
println!("reference count = {}", Rc::strong_count(&sun)); // 4 references
// TODO
drop(earth);
println!("reference count = {}", Rc::strong_count(&sun)); // 3 references
// TODO
drop(venus);
println!("reference count = {}", Rc::strong_count(&sun)); // 2 references
// TODO
drop(mercury);
println!("reference count = {}", Rc::strong_count(&sun)); // 1 reference
assert_eq!(Rc::strong_count(&sun), 1);
}
Rc::new(Sun {});
ではなくRc::clone(&sun);
でsun
のクローンを作成することで、sun
の参照を増やすことができる。
drop
を使うことで参照を減らすことができる。
参考
cow1.rs
use std::borrow::Cow;
fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
for i in 0..input.len() {
let v = input[i];
if v < 0 {
// Clones into a vector if not already owned.
input.to_mut()[i] = -v;
}
}
input
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference_mutation() -> Result<(), &'static str> {
// Clone occurs because `input` needs to be mutated.
let slice = [-1, 0, 1];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value"),
}
}
#[test]
fn reference_no_mutation() -> Result<(), &'static str> {
// No clone occurs because `input` doesn't need to be mutated.
let slice = [0, 1, 2];
let mut input = Cow::from(&slice[..]);
match abs_all(&mut input) {
// TODO
Cow::Borrowed(_) => Ok(()),
_ => Err("Expected borrowed value"),
}
}
#[test]
fn owned_no_mutation() -> Result<(), &'static str> {
// We can also pass `slice` without `&` so Cow owns it directly. In this
// case no mutation occurs and thus also no clone, but the result is
// still owned because it was never borrowed or mutated.
let slice = vec![0, 1, 2];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value after mutation"),
}
}
#[test]
fn owned_mutation() -> Result<(), &'static str> {
// Of course this is also the case if a mutation does occur. In this
// case the call to `to_mut()` in the abs_all() function returns a
// reference to the same data as before.
let slice = vec![-1, 0, 1];
let mut input = Cow::from(slice);
match abs_all(&mut input) {
// TODO
Cow::Owned(_) => Ok(()),
_ => Err("Expected owned value after mutation"),
}
}
}
abs_all
は絶対値を求める関数で、input
の値が負の場合は絶対値に変換する。to_mut()
はCow
がBorrowed
の場合はOwned
に変換する。
reference_mutation
はマイナスの値があるので、変換が生じCow::Owned
が返される。
reference_no_mutation
はマイナスの値もなく、変換が必要ないのでCow::Borrowed
が返される。
owned_no_mutation
はCow::from(slice)
でCow
がはじめから所有するのでCow::Owned
が返される。
参考

Threads
threads1.rs
use std::thread;
use std::time::{Duration, Instant};
fn main() {
let mut handles = vec![];
for i in 0..10 {
handles.push(thread::spawn(move || {
let start = Instant::now();
thread::sleep(Duration::from_millis(250));
println!("thread {} is complete", i);
start.elapsed().as_millis()
}));
}
let mut results: Vec<u128> = vec![];
for handle in handles {
// TODO: a struct is returned from thread::spawn, can you use it?
results.push(handle.join().unwrap())
}
if results.len() != 10 {
panic!("Oh no! All the spawned threads did not finish!");
}
println!();
for (i, result) in results.into_iter().enumerate() {
println!("thread {} took {}ms", i, result);
}
}
join
メソッドでスレッドの終了を待つ。
参考
threads2.rs
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
struct JobStatus {
jobs_completed: u32,
}
fn main() {
let status = Arc::new(Mutex::new(JobStatus { jobs_completed: 0 }));
let mut handles = vec![];
for _ in 0..10 {
let status_shared = Arc::clone(&status);
let handle = thread::spawn(move || {
thread::sleep(Duration::from_millis(250));
// TODO: You must take an action before you update a shared value
let mut status = status_shared.lock().unwrap();
status.jobs_completed += 1;
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
// TODO: Print the value of the JobStatus.jobs_completed. Did you notice
// anything interesting in the output? Do you have to 'join' on all the
// handles?
}
println!("jobs completed {}", status.lock().unwrap().jobs_completed)
}
Arc
とMutex
を使って複数のスレッド間で共有される状態を更新する。
Mutex
は、1度に1つのスレッドだけが共有データにアクセスできるようにすることで、データ競合を防ぐ。スレッドでデータにアクセするためにはlock
を取得する必要がある。
Arc
は、複数のスレッド間で所有権を共有する。
参考
threads3.rs
use std::sync::mpsc;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
struct Queue {
length: u32,
first_half: Vec<u32>,
second_half: Vec<u32>,
}
impl Queue {
fn new() -> Self {
Queue {
length: 10,
first_half: vec![1, 2, 3, 4, 5],
second_half: vec![6, 7, 8, 9, 10],
}
}
}
fn send_tx(q: Queue, tx: mpsc::Sender<u32>) -> () {
let qc = Arc::new(q);
let qc1 = Arc::clone(&qc);
let qc2 = Arc::clone(&qc);
let tx_clone = mpsc::Sender::clone(&tx);
thread::spawn(move || {
for val in &qc1.first_half {
println!("sending {:?}", val);
tx.send(*val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
thread::spawn(move || {
for val in &qc2.second_half {
println!("sending {:?}", val);
tx_clone.send(*val).unwrap();
thread::sleep(Duration::from_secs(1));
}
});
}
#[test]
fn main() {
let (tx, rx) = mpsc::channel();
let queue = Queue::new();
let queue_length = queue.length;
send_tx(queue, tx);
let mut total_received: u32 = 0;
for received in rx {
println!("Got: {}", received);
total_received += 1;
}
println!("total numbers received: {}", total_received);
assert_eq!(total_received, queue_length)
}
mpsc::Sender::clone
を使ってtx
を複製することで、複数のスレッドから同じチャンネルに送信できる。
参考

Macros
macros1.rs
macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}
fn main() {
my_macro!();
}
マクロの呼び出しは、!
を使って行う。
参考
macros2.rs
macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}
fn main() {
my_macro!();
}
マクロは呼び出す前に定義されている、もしくはスコープに取り込む必要がある。
参考
macros3.rs
mod macros {
#[macro_export]
macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}
}
fn main() {
my_macro!();
}
#[macro_export]
を使うことで、マクロをモジュールの外から利用できるようにする。
参考
macros4.rs
#[rustfmt::skip]
macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
($val:expr) => {
println!("Look at this other macro: {}", $val);
}
}
fn main() {
my_macro!();
my_macro!(7777);
}
マクロでオーバーロードする時、それぞれのアームは;
で区切る。
参考

Clippy
clippy1.rs
use std::f32;
fn main() {
let pi = f32::consts::PI;
let radius = 5.00f32;
let area = pi * f32::powi(radius, 2);
println!(
"The area of a circle with radius {:.2} is {:.5}!",
radius, area
)
}
Clippy
はlintツール。円周率の定数を使用するように修正する。
参考
- https://doc.rust-lang.org/stable/book/appendix-04-useful-development-tools.html#more-lints-with-clippy
- https://github.com/rust-lang/rust-clippy
clippy2.rs
fn main() {
let mut res = 42;
let option = Some(12);
if let Some(x) = option {
res += x;
}
println!("{}", res);
}
if let
を使うようにClippy
が警告するので、そのように修正する。
clippy3.rs
#[allow(unused_variables, unused_assignments)]
fn main() {
let my_option: Option<()> = None;
if my_option.is_none() {
// my_option.unwrap();
println!("This option doesn't contain a value!");
}
let my_arr = &[-1, -2, -3, -4, -5, -6];
println!("My array! Here it is: {:?}", my_arr);
let mut my_empty_vec = vec![1, 2, 3, 4, 5];
my_empty_vec.clear();
println!("This Vec is empty, see? {:?}", my_empty_vec);
let mut value_a = 45;
let mut value_b = 66;
// Let's swap these two!
std::mem::swap(&mut value_a, &mut value_b);
println!("value a: {}; value b: {}", value_a, value_b);
}
Clippy
が警告を出すので、各々それにしたがってコードを修正する。

Type conversions
using_as
fn average(values: &[f64]) -> f64 {
let total = values.iter().sum::<f64>();
total / (values.len() as f64)
}
fn main() {
let values = [3.5, 0.3, 13.0, 11.7];
println!("{}", average(&values));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn returns_proper_type_and_value() {
assert_eq!(average(&[3.5, 0.3, 13.0, 11.7]), 7.125);
}
}
as
を使って型をf64
に変換する。
参考
- https://doc.rust-lang.org/std/keyword.as.html
- https://doc.rust-lang.org/stable/book/appendix-01-keywords.html#keywords-currently-in-use
from_into.rs
#[derive(Debug)]
struct Person {
name: String,
age: usize,
}
// We implement the Default trait to use it as a fallback
// when the provided string is not convertible into a Person object
impl Default for Person {
fn default() -> Person {
Person {
name: String::from("John"),
age: 30,
}
}
}
// Your task is to complete this implementation in order for the line `let p =
// Person::from("Mark,20")` to compile Please note that you'll need to parse the
// age component into a `usize` with something like `"4".parse::<usize>()`. The
// outcome of this needs to be handled appropriately.
//
// Steps:
// 1. If the length of the provided string is 0, then return the default of Person.
// 2. Split the given string on the commas present in it.
// 3. Extract the first element from the split operation and use it as the name.
// 4. If the name is empty, then return the default of Person.
// 5. Extract the other element from the split operation and parse it into a `usize` as the age.
// If while parsing the age, something goes wrong, then return the default of
// Person Otherwise, then return an instantiated Person object with the results
impl From<&str> for Person {
fn from(s: &str) -> Person {
if s.is_empty() {
return Person::default();
}
let parts: Vec<&str> = s.split(',').collect();
if parts.len() < 2 {
return Person::default();
}
let name = parts[0].to_string();
if name.is_empty() {
return Person::default();
}
match parts[1].parse::<usize>() {
Ok(age) => Person { name, age },
Err(_) => Person::default(),
}
}
}
fn main() {
// Use the `from` function
let p1 = Person::from("Mark,20");
// Since From is implemented for Person, we should be able to use Into
let p2: Person = "Gerald,70".into();
println!("{:?}", p1);
println!("{:?}", p2);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default() {
// Test that the default person is 30 year old John
let dp = Person::default();
assert_eq!(dp.name, "John");
assert_eq!(dp.age, 30);
}
#[test]
fn test_bad_convert() {
// Test that John is returned when bad string is provided
let p = Person::from("");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_good_convert() {
// Test that "Mark,20" works
let p = Person::from("Mark,20");
assert_eq!(p.name, "Mark");
assert_eq!(p.age, 20);
}
#[test]
fn test_bad_age() {
// Test that "Mark,twenty" will return the default person due to an
// error in parsing age
let p = Person::from("Mark,twenty");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_comma_and_age() {
let p: Person = Person::from("Mark");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_age() {
let p: Person = Person::from("Mark,");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_name() {
let p: Person = Person::from(",1");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_name_and_age() {
let p: Person = Person::from(",");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_missing_name_and_invalid_age() {
let p: Person = Person::from(",one");
assert_eq!(p.name, "John");
assert_eq!(p.age, 30);
}
#[test]
fn test_trailing_comma() {
let p: Person = Person::from("Mike,32,");
assert_eq!(p.name, "Mike");
assert_eq!(p.age, 32);
}
#[test]
fn test_trailing_comma_and_some_string() {
let p: Person = Person::from("Mike,32,man");
assert_eq!(p.name, "Mike");
assert_eq!(p.age, 32);
}
}
// 1. If the length of the provided string is 0, then return the default of Person.
// 2. Split the given string on the commas present in it.
// 3. Extract the first element from the split operation and use it as the name.
// 4. If the name is empty, then return the default of Person.
// 5. Extract the other element from the split operation and parse it into ausize
as the age.
上記の手順をForm
トレイトで実装する。
参考
- https://doc.rust-lang.org/std/convert/trait.From.html
- https://doc.rust-lang.org/rust-by-example/conversion/from_into.html
from_str.rs
use std::num::ParseIntError;
use std::str::FromStr;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: usize,
}
// We will use this error type for the `FromStr` implementation.
#[derive(Debug, PartialEq)]
enum ParsePersonError {
// Empty input string
Empty,
// Incorrect number of fields
BadLen,
// Empty name field
NoName,
// Wrapped error from parse::<usize>()
ParseInt(ParseIntError),
}
// Steps:
// 1. If the length of the provided string is 0, an error should be returned
// 2. Split the given string on the commas present in it
// 3. Only 2 elements should be returned from the split, otherwise return an error
// 4. Extract the first element from the split operation and use it as the name
// 5. Extract the other element from the split operation and parse it into a `usize` as the age with something like `"4".parse::<usize>()`
// 6. If while extracting the name and the age something goes wrong, an error should be returned
// If everything goes well, then return a Result of a Person object
//
// As an aside: `Box<dyn Error>` implements `From<&'_ str>`. This means that if
// you want to return a string error message, you can do so via just using
// return `Err("my error message".into())`.
impl FromStr for Person {
type Err = ParsePersonError;
fn from_str(s: &str) -> Result<Person, Self::Err> {
if s.is_empty() {
return Err(ParsePersonError::Empty);
}
let parts: Vec<&str> = s.split(',').collect();
if parts.len() != 2 {
return Err(ParsePersonError::BadLen);
}
let name = parts[0].to_string();
if name.is_empty() {
return Err(ParsePersonError::NoName);
}
let age = parts[1]
.parse::<usize>()
.map_err(ParsePersonError::ParseInt)?;
Ok(Person { name, age })
}
}
fn main() {
let p = "Mark,20".parse::<Person>().unwrap();
println!("{:?}", p);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_input() {
assert_eq!("".parse::<Person>(), Err(ParsePersonError::Empty));
}
#[test]
fn good_input() {
let p = "John,32".parse::<Person>();
assert!(p.is_ok());
let p = p.unwrap();
assert_eq!(p.name, "John");
assert_eq!(p.age, 32);
}
#[test]
fn missing_age() {
assert!(matches!(
"John,".parse::<Person>(),
Err(ParsePersonError::ParseInt(_))
));
}
#[test]
fn invalid_age() {
assert!(matches!(
"John,twenty".parse::<Person>(),
Err(ParsePersonError::ParseInt(_))
));
}
#[test]
fn missing_comma_and_age() {
assert_eq!("John".parse::<Person>(), Err(ParsePersonError::BadLen));
}
#[test]
fn missing_name() {
assert_eq!(",1".parse::<Person>(), Err(ParsePersonError::NoName));
}
#[test]
fn missing_name_and_age() {
assert!(matches!(
",".parse::<Person>(),
Err(ParsePersonError::NoName | ParsePersonError::ParseInt(_))
));
}
#[test]
fn missing_name_and_invalid_age() {
assert!(matches!(
",one".parse::<Person>(),
Err(ParsePersonError::NoName | ParsePersonError::ParseInt(_))
));
}
#[test]
fn trailing_comma() {
assert_eq!("John,32,".parse::<Person>(), Err(ParsePersonError::BadLen));
}
#[test]
fn trailing_comma_and_some_string() {
assert_eq!(
"John,32,man".parse::<Person>(),
Err(ParsePersonError::BadLen)
);
}
}
// 1. If the length of the provided string is 0, an error should be returned
// 2. Split the given string on the commas present in it
// 3. Only 2 elements should be returned from the split, otherwise return an error
// 4. Extract the first element from the split operation and use it as the name
// 5. Extract the other element from the split operation and parse it into ausize
as the age with something like"4".parse::<usize>()
// 6. If while extracting the name and the age something goes wrong, an error should be returned
上記の条件に沿ってFromStr
トレイトを実装する。
参考
try_from_into.rs
use std::convert::{TryFrom, TryInto};
#[derive(Debug, PartialEq)]
struct Color {
red: u8,
green: u8,
blue: u8,
}
// We will use this error type for these `TryFrom` conversions.
#[derive(Debug, PartialEq)]
enum IntoColorError {
// Incorrect length of slice
BadLen,
// Integer conversion error
IntConversion,
}
// Your task is to complete this implementation and return an Ok result of inner
// type Color. You need to create an implementation for a tuple of three
// integers, an array of three integers, and a slice of integers.
//
// Note that the implementation for tuple and array will be checked at compile
// time, but the slice implementation needs to check the slice length! Also note
// that correct RGB color values must be integers in the 0..=255 range.
// 範囲チェック関数
fn is_out_of_range(values: &[i16]) -> bool {
values.iter().any(|&x| x < 0 || x > 255)
}
// Tuple implementation
impl TryFrom<(i16, i16, i16)> for Color {
type Error = IntoColorError;
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {
let arr = [tuple.0, tuple.1, tuple.2];
if is_out_of_range(&arr) {
return Err(IntoColorError::IntConversion);
}
Ok(Color {
red: tuple.0 as u8,
green: tuple.1 as u8,
blue: tuple.2 as u8,
})
}
}
// Array implementation
impl TryFrom<[i16; 3]> for Color {
type Error = IntoColorError;
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {
if is_out_of_range(&arr) {
return Err(IntoColorError::IntConversion);
}
Ok(Color {
red: arr[0] as u8,
green: arr[1] as u8,
blue: arr[2] as u8,
})
}
}
// Slice implementation
impl TryFrom<&[i16]> for Color {
type Error = IntoColorError;
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {
if slice.len() != 3 {
return Err(IntoColorError::BadLen);
}
if is_out_of_range(slice) {
return Err(IntoColorError::IntConversion);
}
Ok(Color {
red: slice[0] as u8,
green: slice[1] as u8,
blue: slice[2] as u8,
})
}
}
fn main() {
// Use the `try_from` function
let c1 = Color::try_from((183, 65, 14));
println!("{:?}", c1);
// Since TryFrom is implemented for Color, we should be able to use TryInto
let c2: Result<Color, _> = [183, 65, 14].try_into();
println!("{:?}", c2);
let v = vec![183, 65, 14];
// With slice we should use `try_from` function
let c3 = Color::try_from(&v[..]);
println!("{:?}", c3);
// or take slice within round brackets and use TryInto
let c4: Result<Color, _> = (&v[..]).try_into();
println!("{:?}", c4);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tuple_out_of_range_positive() {
assert_eq!(
Color::try_from((256, 1000, 10000)),
Err(IntoColorError::IntConversion)
);
}
#[test]
fn test_tuple_out_of_range_negative() {
assert_eq!(
Color::try_from((-1, -10, -256)),
Err(IntoColorError::IntConversion)
);
}
#[test]
fn test_tuple_sum() {
assert_eq!(
Color::try_from((-1, 255, 255)),
Err(IntoColorError::IntConversion)
);
}
#[test]
fn test_tuple_correct() {
let c: Result<Color, _> = (183, 65, 14).try_into();
assert!(c.is_ok());
assert_eq!(
c.unwrap(),
Color {
red: 183,
green: 65,
blue: 14
}
);
}
#[test]
fn test_array_out_of_range_positive() {
let c: Result<Color, _> = [1000, 10000, 256].try_into();
assert_eq!(c, Err(IntoColorError::IntConversion));
}
#[test]
fn test_array_out_of_range_negative() {
let c: Result<Color, _> = [-10, -256, -1].try_into();
assert_eq!(c, Err(IntoColorError::IntConversion));
}
#[test]
fn test_array_sum() {
let c: Result<Color, _> = [-1, 255, 255].try_into();
assert_eq!(c, Err(IntoColorError::IntConversion));
}
#[test]
fn test_array_correct() {
let c: Result<Color, _> = [183, 65, 14].try_into();
assert!(c.is_ok());
assert_eq!(
c.unwrap(),
Color {
red: 183,
green: 65,
blue: 14
}
);
}
#[test]
fn test_slice_out_of_range_positive() {
let arr = [10000, 256, 1000];
assert_eq!(
Color::try_from(&arr[..]),
Err(IntoColorError::IntConversion)
);
}
#[test]
fn test_slice_out_of_range_negative() {
let arr = [-256, -1, -10];
assert_eq!(
Color::try_from(&arr[..]),
Err(IntoColorError::IntConversion)
);
}
#[test]
fn test_slice_sum() {
let arr = [-1, 255, 255];
assert_eq!(
Color::try_from(&arr[..]),
Err(IntoColorError::IntConversion)
);
}
#[test]
fn test_slice_correct() {
let v = vec![183, 65, 14];
let c: Result<Color, _> = Color::try_from(&v[..]);
assert!(c.is_ok());
assert_eq!(
c.unwrap(),
Color {
red: 183,
green: 65,
blue: 14
}
);
}
#[test]
fn test_slice_excess_length() {
let v = vec![0, 0, 0, 0];
assert_eq!(Color::try_from(&v[..]), Err(IntoColorError::BadLen));
}
#[test]
fn test_slice_insufficient_length() {
let v = vec![0, 0];
assert_eq!(Color::try_from(&v[..]), Err(IntoColorError::BadLen));
}
}
Color
構造体に対してTryFrom
トレイトを実装する。Color
構造体のred
, green
, blue
の値が0..=255
の範囲に収まっているかをチェックする。
参考
- https://doc.rust-lang.org/std/convert/trait.TryFrom.html
- https://doc.rust-lang.org/rust-by-example/conversion/try_from_try_into.html
as_ref_mut.rs
// Obtain the number of bytes (not characters) in the given argument.
// TODO: Add the AsRef trait appropriately as a trait bound.
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().as_bytes().len()
}
// Obtain the number of characters (not bytes) in the given argument.
// TODO: Add the AsRef trait appropriately as a trait bound.
fn char_counter<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().chars().count()
}
// Squares a number using as_mut().
// TODO: Add the appropriate trait bound.
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
// TODO: Implement the function body.
let num = arg.as_mut();
*num = *num * *num;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn different_counts() {
let s = "Café au lait";
assert_ne!(char_counter(s), byte_counter(s));
}
#[test]
fn same_counts() {
let s = "Cafe au lait";
assert_eq!(char_counter(s), byte_counter(s));
}
#[test]
fn different_counts_using_string() {
let s = String::from("Café au lait");
assert_ne!(char_counter(s.clone()), byte_counter(s));
}
#[test]
fn same_counts_using_string() {
let s = String::from("Cafe au lait");
assert_eq!(char_counter(s.clone()), byte_counter(s));
}
#[test]
fn mut_box() {
let mut num: Box<u32> = Box::new(3);
num_sq(&mut num);
assert_eq!(*num, 9);
}
}
as_ref
で参照を取得し、as_mut
で可変参照を取得する。as_ref
を使うためにはAsRef
トレイトが、as_mut
を使うためにはAsMut
トレイトが必要なので、トレイト境界を追加する。
参考