mirror of https://github.com/01-edu/public.git
Augusto
4 years ago
4 changed files with 97 additions and 0 deletions
@ -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", |
||||||
|
] |
@ -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"} |
@ -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); |
||||||
|
} |
@ -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…
Reference in new issue