From 02abe0d86d438185ad7c0e7ede3027d7dbc4442d Mon Sep 17 00:00:00 2001 From: Augusto Date: Fri, 25 Dec 2020 22:29:12 +0000 Subject: [PATCH] Add exercise `borrow`'s subject and tests --- rust/tests/borrow_test/Cargo.lock | 12 +++++ rust/tests/borrow_test/Cargo.toml | 10 ++++ rust/tests/borrow_test/src/main.rs | 85 ++++++++++++++++++++++++++++++ subjects/borrow/README.md | 35 ++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 rust/tests/borrow_test/Cargo.lock create mode 100644 rust/tests/borrow_test/Cargo.toml create mode 100644 rust/tests/borrow_test/src/main.rs create mode 100644 subjects/borrow/README.md diff --git a/rust/tests/borrow_test/Cargo.lock b/rust/tests/borrow_test/Cargo.lock new file mode 100644 index 00000000..0ca31837 --- /dev/null +++ b/rust/tests/borrow_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "borrow" +version = "0.1.0" + +[[package]] +name = "borrow_test" +version = "0.1.0" +dependencies = [ + "borrow", +] diff --git a/rust/tests/borrow_test/Cargo.toml b/rust/tests/borrow_test/Cargo.toml new file mode 100644 index 00000000..b13a6d97 --- /dev/null +++ b/rust/tests/borrow_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "borrow_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +borrow = { path = "../../../../rust-piscine-solutions/borrow"} \ No newline at end of file diff --git a/rust/tests/borrow_test/src/main.rs b/rust/tests/borrow_test/src/main.rs new file mode 100644 index 00000000..c5200a71 --- /dev/null +++ b/rust/tests/borrow_test/src/main.rs @@ -0,0 +1,85 @@ +/* +## borrow + +### Instructions + +Complete the signature and the body of the `str_len` function so it +receives a string or a string literal and returns its length (of type usize) +without taking ownership of the value (i.e, borrowing the value) + +### Example + +```rust +fn main() { + let s = "hello"; + let s1 = "camelCase".to_string(); + + println!("\tstr_len(\"{}\") = {}", s, str_len(s)); + println!("\tstr_len(\"{}\") = {}", s1, str_len(&s1)); +} +``` +*/ + +use borrow::*; + +fn main() { + let s = "hello"; + let s1 = "camelCase".to_string(); + + println!("\tstr_len(\"{}\") = {}", s, str_len(s)); + println!("\tstr_len(\"{}\") = {}", s1, str_len(&s1)); +} + +#[test] +// maybe not the best way to make the test, but I wanted to use +// lifetimes +fn str_len_test() { + struct TstLit<'a> { + str: &'a str, + l: usize, + } + + struct TstString { + str: String, + l: usize, + } + + let tsts = vec![ + TstLit { str: "hello", l: 5 }, + TstLit { str: "how", l: 3 }, + TstLit { + str: "are you", + l: 7, + }, + TstLit { + str: "change", + l: 6, + }, + ]; + let o_tsts = vec![ + TstString { + str: "hello".to_string(), + l: 5, + }, + TstString { + str: "how".to_string(), + l: 3, + }, + TstString { + str: "are you".to_string(), + l: 7, + }, + TstString { + str: "change".to_string(), + l: 6, + }, + ]; + + for t in tsts.iter() { + assert_eq!(t.l, str_len(t.str)); + } + + for t in o_tsts.iter() { + assert_eq!(t.l, str_len(&t.str)); + } +} diff --git a/subjects/borrow/README.md b/subjects/borrow/README.md new file mode 100644 index 00000000..12c24715 --- /dev/null +++ b/subjects/borrow/README.md @@ -0,0 +1,35 @@ +## borrow + +### Instructions + +Complete the signature and the body of the `str_len` that receives a string or a string literal and returns its length without taking ownership of the value (i.e, borrowing the value) + +### Expected Function + +```rust +pub fn str_len(s: ) -> usize { +} +``` + +### Usage + +Here is a possible program to test your function : + +```rust +fn main() { + let s = "hello"; + let s1 = "camelCase".to_string(); + + println!("\tstr_len(\"{}\") = {}", s, str_len(s)); + println!("\tstr_len(\"{}\") = {}", s1, str_len(&s1)); +} +``` + +And its output: + +```rust +student@ubuntu:~/[[ROOT]]/test$ cargo run +str_len("hello") = 5 +str_len("camelCase") = 9 +student@ubuntu:~/[[ROOT]]/test$ +```