Browse Source

Add subject and test of exercise `strings`

content-update
Augusto 3 years ago
parent
commit
6b31796c78
  1. 12
      rust/tests/strings_test/Cargo.lock
  2. 10
      rust/tests/strings_test/Cargo.toml
  3. 39
      rust/tests/strings_test/src/main.rs
  4. 36
      subjects/strings/README.md

12
rust/tests/strings_test/Cargo.lock diff.generated

@ -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",
]

10
rust/tests/strings_test/Cargo.toml

@ -0,0 +1,10 @@
[package]
name = "strings_test"
version = "0.1.0"
authors = ["Augusto <aug.ornelas@gmail.com>"]
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"}

39
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);
}

36
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$
```
Loading…
Cancel
Save