From 6b31796c781abd570ff588f5cee49eb7444468d5 Mon Sep 17 00:00:00 2001 From: Augusto Date: Sun, 27 Dec 2020 11:15:09 +0000 Subject: [PATCH] Add subject and test of exercise `strings` --- rust/tests/strings_test/Cargo.lock | 12 +++++++++ rust/tests/strings_test/Cargo.toml | 10 ++++++++ rust/tests/strings_test/src/main.rs | 39 +++++++++++++++++++++++++++++ subjects/strings/README.md | 36 ++++++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 rust/tests/strings_test/Cargo.lock create mode 100644 rust/tests/strings_test/Cargo.toml create mode 100644 rust/tests/strings_test/src/main.rs create mode 100644 subjects/strings/README.md diff --git a/rust/tests/strings_test/Cargo.lock b/rust/tests/strings_test/Cargo.lock new file mode 100644 index 00000000..2c288afa --- /dev/null +++ b/rust/tests/strings_test/Cargo.lock @@ -0,0 +1,12 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "strings" +version = "0.1.0" + +[[package]] +name = "strings_test" +version = "0.1.0" +dependencies = [ + "strings", +] diff --git a/rust/tests/strings_test/Cargo.toml b/rust/tests/strings_test/Cargo.toml new file mode 100644 index 00000000..0cefe153 --- /dev/null +++ b/rust/tests/strings_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "strings_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] +strings = { path = "../../../../rust-piscine-solutions/strings"} \ No newline at end of file diff --git a/rust/tests/strings_test/src/main.rs b/rust/tests/strings_test/src/main.rs new file mode 100644 index 00000000..25b8099b --- /dev/null +++ b/rust/tests/strings_test/src/main.rs @@ -0,0 +1,39 @@ +// Write a function that receives a string slice and returns the +// length of character of the string + +use strings::*; + +fn main() { + println!("lenght of {} = {}", "❤", "❤".len()); + println!("lenght of {} = {}", "❤", char_lenght("❤")); + println!("lenght of {} = {}", "形声字", char_lenght("形聲字")); + println!("lenght of {} = {}", "形声字", "形聲字".len()); + println!("lenght of {} = {}", "change", "change".len()); + println!("lenght of {} = {}", "change", char_lenght("change")); + println!("char lenght of {} = {}", "😍", char_lenght("😍")); +} + +// fn char_lenght(s: &str) -> usize { +// let mut chars = 0; +// for _ in s.chars() { +// chars += 1; +// } +// chars +// } + +#[test] +fn test_ascii() { + let s = "ascii"; + assert_eq!(char_lenght(s), 5); +} + +#[test] +fn test_emoji() { + let s = "❤😍"; + assert_eq!(char_lenght(s), 2); +} +#[test] +fn test_chinese_char() { + let s = "形声字"; + assert_eq!(char_lenght(s), 3); +} diff --git a/subjects/strings/README.md b/subjects/strings/README.md new file mode 100644 index 00000000..8c68e9fa --- /dev/null +++ b/subjects/strings/README.md @@ -0,0 +1,36 @@ +## strings + +### Instructions + +Write a function that receives a string slice and returns the number of character of the string + +### Expected Function + +```rust +fn char_length(s: &str) -> usize { +} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + println!("length of {} = {}", "❤", char_length("❤")); + println!("length of {} = {}", "形声字", char_length("形聲字")); + println!("length of {} = {}", "change", char_length("change")); + println!("length of {} = {}", "😍", char_length("😍")); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +length of ❤ = 1 +length of 形声字 = 3 +length of change = 6 +length of 😍 = 1 +student@ubuntu:~/[[ROOT]]/test$ +```