From 9c9d734ce65eab3a4ef8b1e0ec2366e4552dab87 Mon Sep 17 00:00:00 2001 From: Tsuyoshi Maeda Date: Sat, 2 Dec 2017 09:56:43 +0900 Subject: [PATCH 1/4] Modified README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7fd70b0..325c73b 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ function add(a, b) { return a + b; } -function output(input) { +function output_string(input) { console.log(input); } From 4e012a6d7db04857fa5f69be1f1df4aa70e09241 Mon Sep 17 00:00:00 2001 From: Tsuyoshi Maeda Date: Sat, 2 Dec 2017 09:57:20 +0900 Subject: [PATCH 2/4] Translated JavaScript code into Rust --- src/main.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1fd2471 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,55 @@ +fn main() { + let number_1 = 1; + println!("{}", number_1); + + let mut number_2 = 1; + number_2 += 1; + println!("{}", number_2); + + let number_3 = add(1, 2); + println!("{}", number_3); + + output_string("this is input value"); + + let person = Person::new( + "Code", + "Chrysalis", + 2 + ); + println!("{:?}", person); + person.greet(); +} + +fn add(a: i32, b: i32) -> i32 { + a + b +} + +fn output_string(input: &str) -> () { + println!("{}", input); +} + +#[derive(Debug)] +struct Person { + first_name: String, + last_name: String, + age: u8 +} + +impl Person { + fn new(first_name: &str, last_name: &str, age: u8) -> Person { + Person { + first_name: first_name.to_string(), + last_name: last_name.to_string(), + age: age + } + } + + fn greet(&self) -> () { + println!( + "My name is {} {}. I'm {} years old.", + &self.first_name, + &self.last_name, + &self.age + ); + } +} \ No newline at end of file From 40ce41cd2a9de6a001aec1c1e4606db10d77ed51 Mon Sep 17 00:00:00 2001 From: Tsuyoshi Maeda Date: Sat, 2 Dec 2017 10:26:38 +0900 Subject: [PATCH 3/4] Added Test information into README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 325c73b..a75ef4e 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ $ cargo test - [ ] Difine type of return value and arugments on each function/method. - [ ] Write tests for each function/method. - Hint: Do you remember annotation for tests. + - [How to Write Tests](https://doc.rust-lang.org/book/second-edition/ch11-01-writing-tests.html) ```javascript function main() { @@ -125,5 +126,6 @@ class Person { - [Slides for today's lecture](https://docs.google.com/presentation/d/1aG7Uq4D9LPW7FKsi6ImRQ2_SabCbOJcDhABhkb9-vao/edit?usp=sharing) - [Install Rust](https://www.rust-lang.org/en-US/install.html) +- [How to Write Tests](https://doc.rust-lang.org/book/second-edition/ch11-01-writing-tests.html) - [Serde](https://github.com/serde-rs/serde) - [Crates and Modules](https://doc.rust-lang.org/book/first-edition/crates-and-modules.html) \ No newline at end of file From b994d9986785c251b0b6d92b78ba010cb04b5c2f Mon Sep 17 00:00:00 2001 From: Tsuyoshi Maeda Date: Sat, 2 Dec 2017 10:28:54 +0900 Subject: [PATCH 4/4] Added test directory. TODO: use #[cfg(test)] --- Cargo.toml | 2 +- src/main.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2c20f90..3fcda58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "cc2-rust-workshop" +name = "cc2_rust_workshop" version = "0.1.0" authors = ["Tsuyoshi Maeda "] diff --git a/src/main.rs b/src/main.rs index 1fd2471..218a5fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,10 +23,20 @@ fn main() { fn add(a: i32, b: i32) -> i32 { a + b } +#[test] +fn it_add() { + assert_eq!(1 + 2, add(1, 2)); +} fn output_string(input: &str) -> () { println!("{}", input); } +#[test] +fn it_output_string() { + // Expected no error. + output_string("abc"); + assert!(true); +} #[derive(Debug)] struct Person { @@ -52,4 +62,19 @@ impl Person { &self.age ); } +} +#[test] +fn it_person() { + let person = Person::new("FN", "LN", 1); + assert_eq!("FN", person.first_name); + assert_eq!("LN", person.last_name); + assert_eq!(1, person.age); +} +#[test] +fn it_person_greet() { + let person = Person::new("FN", "LN", 1); + + // Expected no error. + person.greet(); + assert!(true); } \ No newline at end of file