Created
March 17, 2021 06:41
-
-
Save yuk1ty/5a9c686d9ec9031d0c4bd95a00bdf5b6 to your computer and use it in GitHub Desktop.
Rustハンズオンで使用したコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 実行時引数を受け取って cat するプログラム | |
fn run(path: String) { | |
match std::fs::read_to_string(path) { | |
Ok(content) => print!("{}", content), | |
Err(why) => println!("{}", why), | |
} | |
} | |
fn main() { | |
let mut args = std::env::args(); | |
match args.nth(1) { | |
Some(path) => run(path), | |
None => println!("1つ目の実行時引数にファイルパスを入れる必要があります。") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// エラー付き割り算 | |
enum CalcError { | |
// ゼロ除算を行った場合に返すエラー | |
DividedByZero, | |
// 負の数が入っていた場合に返すエラー。中にその数値を入れる。 | |
DetectedNegative(i32), | |
} | |
fn division(dividened: i32, divisor: i32) -> Result<i32, CalcError> { | |
if divisor == 0 { | |
return Err(CalcError::DividedByZero); | |
} | |
if dividened < 0 { | |
return Err(CalcError::DetectedNegative(dividened)); | |
} | |
if divisor < 0 { | |
return Err(CalcError::DetectedNegative(divisor)); | |
} | |
Ok(dividened / divisor) | |
} | |
fn main() { | |
let answer = division(4, 2); | |
match answer { | |
Ok(value) => println!("answer is {}", value), | |
Err(err) => match err { | |
// eprintln! マクロはエラー出力をできる。 | |
CalcError::DividedByZero => eprintln!("ゼロ除算です"), | |
CalcError::DetectedNegative(num) => { | |
eprintln!("{} は負の数です。負の数は入れられません。", num) | |
} | |
}, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ライフタイム: エクササイズ1 | |
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ライフタイム: エクササイズ2 | |
struct User { | |
id: UserId, | |
user_name: UserName | |
} | |
impl User { | |
fn new(user_name: &str) -> Self { | |
User { | |
id: UserId(1), | |
user_name: UserName(user_name), | |
} | |
} | |
} | |
struct UserId(i32); | |
struct UserName(&str); | |
fn main() { | |
let user = User::new("namae"); | |
assert_eq!(user.user_name.0, "namae"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 所有権: エクササイズ1 | |
struct User { | |
tag: i32 | |
} | |
impl User { | |
fn new(num: i32) -> User { | |
User { tag: num } | |
} | |
fn print_tag(self) -> i32 { | |
self.tag | |
} | |
} | |
fn main() { | |
let mut user = User::new(1); | |
assert_eq!(user.print_tag(), 1); | |
user.tag = 2; | |
assert_eq!(user.print_tag(), 2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 所有権: エクササイズ2 | |
fn main() { | |
let mut list = vec![]; | |
add_elem(list, 1); | |
add_elem(list, 2); | |
add_elem(list, 3); | |
assert_eq!(list, vec![1, 2, 3]); | |
} | |
fn add_elem(mut target: Vec<i32>, elem: i32) { | |
target.push(elem); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ファイル読み込みのエラーハンドリング | |
fn main() { | |
let path = "./src/main.rs"; | |
match std::fs::read_to_string(path) { | |
Ok(content) => print!("{}", content), | |
Err(why) => println!("{}", why), | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 指定したパスのファイルを読み込む | |
fn main() { | |
let path = "./src/main.rs"; | |
match std::fs::read_to_string(path) { | |
Ok(content) => print!("{}", content), | |
Err(why) => println!("{}", why), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment