summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-09-07 21:16:54 +0530
committerAndinus <andinus@nand.sh>2021-09-07 21:16:54 +0530
commita6a08927f762a0d30c8e199cd2c1386ee2e08d0e (patch)
tree64f405935b716e61e7c9ecb7cc20fdf24f0b2311
parent5622b92d80a80faabc1bcf35c94a35a28ff63e05 (diff)
downloadexercism-a6a08927f762a0d30c8e199cd2c1386ee2e08d0e.tar.gz
Rust: Add Hello World & Lucian's Luscious Lasagna
-rw-r--r--rust/hello-world/.gitignore8
-rw-r--r--rust/hello-world/Cargo.toml4
-rw-r--r--rust/hello-world/GETTING_STARTED.md92
-rw-r--r--rust/hello-world/HELP.md85
-rw-r--r--rust/hello-world/README.md53
-rw-r--r--rust/hello-world/src/lib.rs4
-rw-r--r--rust/hello-world/tests/hello-world.rs4
-rw-r--r--rust/lucians-luscious-lasagna/Cargo.lock5
-rw-r--r--rust/lucians-luscious-lasagna/Cargo.toml4
-rw-r--r--rust/lucians-luscious-lasagna/HELP.md85
-rw-r--r--rust/lucians-luscious-lasagna/HINTS.md31
-rw-r--r--rust/lucians-luscious-lasagna/README.md159
-rw-r--r--rust/lucians-luscious-lasagna/src/lib.rs17
-rw-r--r--rust/lucians-luscious-lasagna/tests/lucians-luscious-lasagna.rs34
14 files changed, 585 insertions, 0 deletions
diff --git a/rust/hello-world/.gitignore b/rust/hello-world/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/rust/hello-world/.gitignore
@@ -0,0 +1,8 @@
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+**/*.rs.bk
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
+Cargo.lock
diff --git a/rust/hello-world/Cargo.toml b/rust/hello-world/Cargo.toml
new file mode 100644
index 0000000..4d9f8bb
--- /dev/null
+++ b/rust/hello-world/Cargo.toml
@@ -0,0 +1,4 @@
+[package]
+edition = "2018"
+name = "hello-world"
+version = "1.1.0"
diff --git a/rust/hello-world/GETTING_STARTED.md b/rust/hello-world/GETTING_STARTED.md
new file mode 100644
index 0000000..04ec562
--- /dev/null
+++ b/rust/hello-world/GETTING_STARTED.md
@@ -0,0 +1,92 @@
+# Getting Started
+
+These exercises lean on Test-Driven Development (TDD), but they're not
+an exact match.
+
+The following steps assume that you are in the same directory as the exercise.
+
+You must have Rust installed.
+Follow the [Installation chapter in the Rust book](https://doc.rust-lang.org/book/ch01-01-installation.html).
+The [Rust language section](http://exercism.io/languages/rust)
+section from exercism is also useful.
+
+## Step 1
+
+Run the test suite. It can be run with `cargo`, which is installed with Rust.
+
+```sh
+$ cargo test
+```
+
+This will compile the `hello-world` crate and run the test, which fails.
+
+```sh
+running 1 test
+test test_hello_world ... FAILED
+
+failures:
+
+---- test_hello_world stdout ----
+thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
+(left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5
+
+failures:
+    test_hello_world
+
+test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured
+```
+
+### Understanding Test Failures
+
+The `test_hello_world` failure states that it is expecting the value,
+`"Hello, World!"`, to be returned from `hello()`.
+The left side of the assertion (at line 5) should be equal to the right side.
+
+```sh
+---- test_hello_world stdout ----
+thread 'test_hello_world' panicked at 'assertion failed: `(left == right)`
+(left: `"Hello, World!"`, right: `"Goodbye, World!"`)', tests/hello-world.rs:5
+```
+
+### Fixing the Error
+
+To fix it, open up `src/lib.rs` and change the `hello` function to return
+`"Hello, World!"` instead of `"Goodbye, World!"`.
+
+```rust
+pub fn hello() -> &'static str {
+    "Hello, World!"
+}
+```
+
+## Step 2
+
+Run the test again. This time, it will pass.
+
+```sh
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
+
+     Running target/debug/deps/hello_world-bd1f06dc726ef14f
+
+running 1 test
+test test_hello_world ... ok
+
+test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
+
+   Doc-tests hello-world
+
+running 0 tests
+
+test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured
+```
+
+## Submit
+
+Once the test is passing, you can submit your code with the following
+command:
+
+```sh
+$ exercism submit src/lib.rs
+```
diff --git a/rust/hello-world/HELP.md b/rust/hello-world/HELP.md
new file mode 100644
index 0000000..b4252f8
--- /dev/null
+++ b/rust/hello-world/HELP.md
@@ -0,0 +1,85 @@
+# Help
+
+## Running the tests
+
+Execute the tests with:
+
+```bash
+$ cargo test
+```
+
+All but the first test have been ignored. After you get the first test to
+pass, open the tests source file which is located in the `tests` directory
+and remove the `#[ignore]` flag from the next test and get the tests to pass
+again. Each separate test is a function with `#[test]` flag above it.
+Continue, until you pass every test.
+
+If you wish to run _only ignored_ tests without editing the tests source file, use:
+
+```bash
+$ cargo test -- --ignored
+```
+
+If you are using Rust 1.51 or later, you can run _all_ tests with
+
+```bash
+$ cargo test -- --include-ignored
+```
+
+To run a specific test, for example `some_test`, you can use:
+
+```bash
+$ cargo test some_test
+```
+
+If the specific test is ignored, use:
+
+```bash
+$ cargo test some_test -- --ignored
+```
+
+To learn more about Rust tests refer to the online [test documentation][rust-tests].
+
+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit src/lib.rs` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+## Rust Installation
+
+Refer to the [exercism help page][help-page] for Rust installation and learning
+resources.
+
+## Submitting the solution
+
+Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
+
+## Feedback, Issues, Pull Requests
+
+The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
+
+If you want to know more about Exercism, take a look at the [contribution guide].
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
+[help-page]: https://exercism.io/tracks/rust/learning
+[github]: https://github.com/exercism/rust
+[contribution guide]: https://exercism.io/docs/community/contributors
\ No newline at end of file
diff --git a/rust/hello-world/README.md b/rust/hello-world/README.md
new file mode 100644
index 0000000..4ece0fa
--- /dev/null
+++ b/rust/hello-world/README.md
@@ -0,0 +1,53 @@
+# Hello World
+
+Welcome to Hello World on Exercism's Rust Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Instructions
+
+The classical introductory exercise. Just say "Hello, World!".
+
+["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
+the traditional first program for beginning programming in a new language
+or environment.
+
+The objectives are simple:
+
+- Write a function that returns the string "Hello, World!".
+- Run the test suite and make sure that it succeeds.
+- Submit your solution and check it at the website.
+
+If everything goes well, you will be ready to fetch your first real exercise.
+
+## Source
+
+### Created by
+
+- @EduardoBautista
+
+### Contributed to by
+
+- @ashleygwilliams
+- @ClashTheBunny
+- @coriolinus
+- @cwhakes
+- @dvoytik
+- @EduardoBautista
+- @efx
+- @ErikSchierboom
+- @hydhknn
+- @IanWhitney
+- @ijanos
+- @kytrinyx
+- @lutostag
+- @nfiles
+- @petertseng
+- @regnerjr
+- @rofrol
+- @stringparser
+- @xakon
+- @ZapAnton
+
+### Based on
+
+This is an exercise to introduce users to using Exercism - http://en.wikipedia.org/wiki/%22Hello,_world!%22_program
\ No newline at end of file
diff --git a/rust/hello-world/src/lib.rs b/rust/hello-world/src/lib.rs
new file mode 100644
index 0000000..5c2db6d
--- /dev/null
+++ b/rust/hello-world/src/lib.rs
@@ -0,0 +1,4 @@
+// &'static is a "lifetime specifier", something you'll learn more about later
+pub fn hello() -> &'static str {
+    "Hello, World!"
+}
diff --git a/rust/hello-world/tests/hello-world.rs b/rust/hello-world/tests/hello-world.rs
new file mode 100644
index 0000000..b29f953
--- /dev/null
+++ b/rust/hello-world/tests/hello-world.rs
@@ -0,0 +1,4 @@
+#[test]
+fn test_hello_world() {
+    assert_eq!("Hello, World!", hello_world::hello());
+}
diff --git a/rust/lucians-luscious-lasagna/Cargo.lock b/rust/lucians-luscious-lasagna/Cargo.lock
new file mode 100644
index 0000000..c5f6eb1
--- /dev/null
+++ b/rust/lucians-luscious-lasagna/Cargo.lock
@@ -0,0 +1,5 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "lucians-luscious-lasagna"
+version = "0.1.0"
diff --git a/rust/lucians-luscious-lasagna/Cargo.toml b/rust/lucians-luscious-lasagna/Cargo.toml
new file mode 100644
index 0000000..75c4718
--- /dev/null
+++ b/rust/lucians-luscious-lasagna/Cargo.toml
@@ -0,0 +1,4 @@
+[package]
+name = "lucians-luscious-lasagna"
+version = "0.1.0"
+edition = "2018"
diff --git a/rust/lucians-luscious-lasagna/HELP.md b/rust/lucians-luscious-lasagna/HELP.md
new file mode 100644
index 0000000..b4252f8
--- /dev/null
+++ b/rust/lucians-luscious-lasagna/HELP.md
@@ -0,0 +1,85 @@
+# Help
+
+## Running the tests
+
+Execute the tests with:
+
+```bash
+$ cargo test
+```
+
+All but the first test have been ignored. After you get the first test to
+pass, open the tests source file which is located in the `tests` directory
+and remove the `#[ignore]` flag from the next test and get the tests to pass
+again. Each separate test is a function with `#[test]` flag above it.
+Continue, until you pass every test.
+
+If you wish to run _only ignored_ tests without editing the tests source file, use:
+
+```bash
+$ cargo test -- --ignored
+```
+
+If you are using Rust 1.51 or later, you can run _all_ tests with
+
+```bash
+$ cargo test -- --include-ignored
+```
+
+To run a specific test, for example `some_test`, you can use:
+
+```bash
+$ cargo test some_test
+```
+
+If the specific test is ignored, use:
+
+```bash
+$ cargo test some_test -- --ignored
+```
+
+To learn more about Rust tests refer to the online [test documentation][rust-tests].
+
+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit src/lib.rs` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+## Rust Installation
+
+Refer to the [exercism help page][help-page] for Rust installation and learning
+resources.
+
+## Submitting the solution
+
+Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
+
+## Feedback, Issues, Pull Requests
+
+The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
+
+If you want to know more about Exercism, take a look at the [contribution guide].
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
+[help-page]: https://exercism.io/tracks/rust/learning
+[github]: https://github.com/exercism/rust
+[contribution guide]: https://exercism.io/docs/community/contributors
\ No newline at end of file
diff --git a/rust/lucians-luscious-lasagna/HINTS.md b/rust/lucians-luscious-lasagna/HINTS.md
new file mode 100644
index 0000000..5d1a5ff
--- /dev/null
+++ b/rust/lucians-luscious-lasagna/HINTS.md
@@ -0,0 +1,31 @@
+# Hints
+
+## General
+
+-   An integer literal can be defined as one or more consecutive digits.
+
+## 1. Define the expected oven time in minutes
+
+-   You need to define a [function][functions] without any parameters.
+
+## 2. Calculate the remaining oven time in minutes
+
+-   You need to define a [function][functions] with a single parameter.
+-   You can use and refer to the previously defined item by its name.
+-   The last expression in a function is [automatically returned][return-values] from the function; you don't have to explicitly indicate which value to return.
+-   You can use the [mathematical operator for subtraction][operators] to subtract values.
+
+## 3. Calculate the preparation time in minutes
+
+-   You need to define a [function][functions] with a single parameter.
+-   You can use the [mathematical operator for multiplicaton][operators] to multiply values.
+
+## 4. Calculate the elapsed time in minutes
+
+-   You need to define a [function][functions] with two parameters.
+-   You can [call][functions] one of the other functions you've defined previously.
+-   You can use the [mathematical operator for addition][operators] to add values.
+
+[functions]: https://doc.rust-lang.org/book/ch03-03-how-functions-work.html
+[return-values]: https://doc.rust-lang.org/book/ch03-03-how-functions-work.html#functions-with-return-values
+[operators]: https://doc.rust-lang.org/book/appendix-02-operators.html
\ No newline at end of file
diff --git a/rust/lucians-luscious-lasagna/README.md b/rust/lucians-luscious-lasagna/README.md
new file mode 100644
index 0000000..2c3fdd3
--- /dev/null
+++ b/rust/lucians-luscious-lasagna/README.md
@@ -0,0 +1,159 @@
+# Lucian's Luscious Lasagna
+
+Welcome to Lucian's Luscious Lasagna on Exercism's Rust Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
+
+## Introduction
+
+In Rust, assigning a value to a name is referred to as a _binding_. Bindings are immutable unless declared with the `mut` keyword. As Rust is a statically-typed language, each binding has a type known at compile-time.
+
+Bindings are most commonly defined using the `let` keyword. Specifying a binding's type is optional for most bindings, as Rust's _type inference_ can usually infer the type based on their value. A binding looks like this:
+
+```rust
+// Automatically inferred type
+let fingers = 10;
+```
+
+Functions are _items_. Where bindings typically refer to a particular value, items refer to a unit of code organization, typically a function or a module, which is available throughout the lifetime of the program. A function automatically returns the result of its last expression. A function may have 0 or more parameters, which are bindings with a lifetime of the function call.
+
+Type inference is theoretically possible for functions, but is disabled as an intentional language design choice. While this means that you need to spend a little more time when writing code to specify precisely what a function's input and output types are, you save the time when you're reading the code, because all the input and output types are explicitly defined.
+
+```rust
+fn add(x: i32, y: i32) -> i32 {
+  x + y
+}
+```
+
+Invoking a function is done by specifying its name followed by parentheses. If the function requires parameters, an argument must be specified for each within the parentheses.
+
+```rust
+let five = add(2, 3);
+```
+
+If a binding's type cannot be inferred, the compiler will report an error. To fix this, add an explicit type annotation to the binding.
+
+```rust
+// Explicit type annotation
+let fingers: i32 = 10;
+```
+
+Items in Rust can be used before or after they are defined, because they have a static lifetime. Bindings, on the other hand, can only be used _after_ they have been defined. Using a binding before it has been defined results in a compile error.
+
+```rust
+fn main() {
+    // `fn add` hasn't yet been defined, but that's perfectly ok
+    dbg!(add(3, 4));
+}
+
+fn add(x: i32, y: i32) -> i32 {
+  x + y
+}
+```
+
+```rust
+// this won't compile; `a` is used before its binding is defined
+let b = a;
+let a = x + y
+```
+
+Rust uses curly braces (`{}`) to define a scope. A binding defined within a scope can't escape from it. This means that scope is defined by indenting the code with spaces, relative to the line declaring the binding.
+
+```rust
+let a = 1;
+dbg!(a); // 1
+{
+    // Here, we re-bind `a` to a new value, which is still immutable.
+    // This technique is called _shadowing_. The new binding is constrained to
+    // this anonymous scope. Outside this scope, the previous binding still
+    // applies.
+    let a = 2;
+    let b = 3;
+    dbg!(a, b); // 2, 3
+}
+// can't use `b` anymore because it is out of scope
+// dbg!(b);
+
+// The shadowed `a` in the inner scope above has fallen out of scope,
+// leaving us with our original binding.
+dbg!(a); // 1
+```
+
+Rust items are often organized in modules. Each crate is implicitly a module, but it can define inner sub-modules of arbitrary depth. A module groups related functionality and is defined using the `mod` keyword.
+
+```rust
+mod calc_i32 {
+    fn add(a: i32, b: i32) -> i32 { a + b }
+    fn sub(a: i32, b: i32) -> i32 { a - b }
+    fn mul(a: i32, b: i32) -> i32 { a * b }
+    fn div(a: i32, b: i32) -> i32 { a / b }
+}
+```
+
+Rust supports two types of comments. The keyword `//` indicates a single-line comment; everything following the keyword until the end of the line is ignored. The keywords `/*` and `*/` indicate a multi-line comment; everything within those two keywords is ignored. It is idiomatic and good practice to prefer single-line comments.
+
+Rust also supports doc-comments, which show up in the generated documentation produced by `cargo doc`. Outer doc comments are formed with the keyword `///`, which acts identically to the `//` keyword. They apply to the item which follows them, such as a function:
+
+```rust
+/// The `add` function produces the sum of its arguments.
+fn add(x: i32, y: i32) -> i32 { x + y }
+```
+
+Inner doc comments are formed with the keyword `//!`, which acts identically to the `//` keyword. They apply to the item enclosing them, such as a module:
+
+```rust
+mod my_cool_module {
+    //! This module is the bee's knees.
+}
+```
+
+Doc comments can be of arbitrary length and contain markdown, which is rendered into the generated documentation.
+
+## Instructions
+
+In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.
+
+You have four tasks, all related to the time spent cooking the lasagna.
+
+## 1. Define the expected oven time in minutes
+
+Define the `expected_minutes_in_oven` binding to check how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40:
+
+```rust
+expected_minutes_in_oven()
+// Returns: 40
+```
+
+## 2. Calculate the remaining oven time in minutes
+
+Define the `remaining_minutes_in_oven` function that takes the actual minutes the lasagna has been in the oven as a parameter and returns how many minutes the lasagna still has to remain in the oven, based on the expected time oven time in minutes from the previous task.
+
+```rust
+remaining_minutes_in_oven(30)
+// Returns: 10
+```
+
+## 3. Calculate the preparation time in minutes
+
+Define the `preparation_time_in_minutes` function that takes the number of layers you added to the lasagna as a parameter and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.
+
+```rust
+preparation_time_in_minutes(2)
+// Returns: 4
+```
+
+## 4. Calculate the elapsed time in minutes
+
+Define the `elapsed_time_in_minutes` function that takes two parameters: the first parameter is the number of layers you added to the lasagna, and the second parameter is the number of minutes the lasagna has been in the oven. The function should return how many minutes you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.
+
+```rust
+elapsed_time_in_minutes(3, 20)
+// Returns: 26
+```
+
+## Source
+
+### Created by
+
+- @coriolinus
+- @ErikSchierboom
\ No newline at end of file
diff --git a/rust/lucians-luscious-lasagna/src/lib.rs b/rust/lucians-luscious-lasagna/src/lib.rs
new file mode 100644
index 0000000..0028195
--- /dev/null
+++ b/rust/lucians-luscious-lasagna/src/lib.rs
@@ -0,0 +1,17 @@
+pub fn expected_minutes_in_oven() -> i32 {
+    return 40;
+}
+
+pub fn remaining_minutes_in_oven(actual_minutes_in_oven: i32) -> i32 {
+    return expected_minutes_in_oven() - actual_minutes_in_oven;
+}
+
+pub fn preparation_time_in_minutes(number_of_layers: i32) -> i32 {
+    return number_of_layers * 2;
+}
+
+pub fn elapsed_time_in_minutes(number_of_layers: i32,
+                               actual_minutes_in_oven: i32) -> i32 {
+    return preparation_time_in_minutes(number_of_layers)
+        + actual_minutes_in_oven;
+}
diff --git a/rust/lucians-luscious-lasagna/tests/lucians-luscious-lasagna.rs b/rust/lucians-luscious-lasagna/tests/lucians-luscious-lasagna.rs
new file mode 100644
index 0000000..340d476
--- /dev/null
+++ b/rust/lucians-luscious-lasagna/tests/lucians-luscious-lasagna.rs
@@ -0,0 +1,34 @@
+use lucians_luscious_lasagna::{
+    elapsed_time_in_minutes, expected_minutes_in_oven, preparation_time_in_minutes,
+    remaining_minutes_in_oven,
+};
+
+#[test]
+fn expected_minutes_in_oven_is_correct() {
+    assert_eq!(40, expected_minutes_in_oven());
+}
+
+#[test]
+fn remaining_minutes_in_oven_after_fifteen_minutes() {
+    assert_eq!(15, remaining_minutes_in_oven(25));
+}
+
+#[test]
+fn preparation_time_in_minutes_for_one_layer() {
+    assert_eq!(2, preparation_time_in_minutes(1));
+}
+
+#[test]
+fn preparation_time_in_minutes_for_multiple_layers() {
+    assert_eq!(8, preparation_time_in_minutes(4));
+}
+
+#[test]
+fn elapsed_time_in_minutes_for_one_layer() {
+    assert_eq!(32, elapsed_time_in_minutes(1, 30));
+}
+
+#[test]
+fn elapsed_time_in_minutes_for_multiple_layers() {
+    assert_eq!(16, elapsed_time_in_minutes(4, 8));
+}