summaryrefslogtreecommitdiff
path: root/exercises/functions
diff options
context:
space:
mode:
authorolivia <olivia@fastmail.com>2018-11-09 20:31:14 +0100
committerolivia <olivia@fastmail.com>2018-11-09 20:31:14 +0100
commitf7846af7ac388652a6f80a2bbce926ba8f053062 (patch)
tree954ee36257047ac612654c5f35e18ed27deda97f /exercises/functions
parent850a13e9133fedb2fce27884902e0aab94da9692 (diff)
right let's try this one again
Diffstat (limited to 'exercises/functions')
-rwxr-xr-xexercises/functions/functions1.rs44
-rwxr-xr-xexercises/functions/functions2.rs42
-rwxr-xr-xexercises/functions/functions3.rs42
-rwxr-xr-xexercises/functions/functions4.rs44
-rwxr-xr-xexercises/functions/functions5.rs47
5 files changed, 219 insertions, 0 deletions
diff --git a/exercises/functions/functions1.rs b/exercises/functions/functions1.rs
new file mode 100755
index 0000000..396dd56
--- /dev/null
+++ b/exercises/functions/functions1.rs
@@ -0,0 +1,44 @@
+// functions1.rs
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ call_me();
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// This main function is calling a function that it expects to exist, but the
+// function doesn't exist. It expects this function to have the name `call_me`.
+// It expects this function to not take any arguments and not return a value.
+// Sounds a lot like `main`, doesn't it?
diff --git a/exercises/functions/functions2.rs b/exercises/functions/functions2.rs
new file mode 100755
index 0000000..1cf95c3
--- /dev/null
+++ b/exercises/functions/functions2.rs
@@ -0,0 +1,42 @@
+// functions2.rs
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ call_me(3);
+}
+
+fn call_me(num) {
+ for i in 0..num {
+ println!("Ring! Call number {}", i + 1);
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// Rust requires that all parts of a function's signature have type annotations,
+// but `call_me` is missing the type annotation of `num`.
diff --git a/exercises/functions/functions3.rs b/exercises/functions/functions3.rs
new file mode 100755
index 0000000..b17543b
--- /dev/null
+++ b/exercises/functions/functions3.rs
@@ -0,0 +1,42 @@
+// functions3.rs
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ call_me();
+}
+
+fn call_me(num: i32) {
+ for i in 0..num {
+ println!("Ring! Call number {}", i + 1);
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// This time, the function *declaration* is okay, but there's something wrong
+// with the place where we're calling the function.
diff --git a/exercises/functions/functions4.rs b/exercises/functions/functions4.rs
new file mode 100755
index 0000000..5baca0e
--- /dev/null
+++ b/exercises/functions/functions4.rs
@@ -0,0 +1,44 @@
+// functions4.rs
+// Make me compile! Scroll down for hints :)
+
+// This store is having a sale where if the price is an even number, you get
+// 10 (money unit) off, but if it's an odd number, it's 3 (money unit) less.
+
+fn main() {
+ let original_price = 51;
+ println!("Your sale price is {}", sale_price(original_price));
+}
+
+fn sale_price(price: i32) -> {
+ if is_even(price) {
+ price - 10
+ } else {
+ price - 3
+ }
+}
+
+fn is_even(num: i32) -> bool {
+ num % 2 == 0
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// The error message points to line 12 and says it expects a type after the
+// `->`. This is where the function's return type should be-- take a look at
+// the `is_even` function for an example!
diff --git a/exercises/functions/functions5.rs b/exercises/functions/functions5.rs
new file mode 100755
index 0000000..d3ff002
--- /dev/null
+++ b/exercises/functions/functions5.rs
@@ -0,0 +1,47 @@
+// functions5.rs
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ let answer = square(3);
+ println!("The answer is {}", answer);
+}
+
+fn square(num: i32) -> i32 {
+ num * num;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// This is a really common error that can be fixed by removing one character.
+// It happens because Rust distinguishes between expressions and statements: expressions return
+// a value based on its operand, and statements simply return a () type which behaves just like `void` in C/C++ language.
+// We want to return a value of `i32` type from the `square` function, but it is returning a `()` type...
+// They are not the same. There are two solutions:
+// 1. Add a `return` ahead of `num * num;`
+// 2. remove `;`, make it to be `num * num`