forked from root/public
Augusto
3 years ago
4 changed files with 129 additions and 0 deletions
@ -0,0 +1,12 @@
|
||||
# This file is automatically @generated by Cargo. |
||||
# It is not intended for manual editing. |
||||
[[package]] |
||||
name = "ownership" |
||||
version = "0.1.0" |
||||
|
||||
[[package]] |
||||
name = "ownership_test" |
||||
version = "0.1.0" |
||||
dependencies = [ |
||||
"ownership", |
||||
] |
@ -0,0 +1,10 @@
|
||||
[package] |
||||
name = "ownership_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] |
||||
ownership = { path = "../../../../rust-piscine-solutions/ownership"} |
@ -0,0 +1,64 @@
|
||||
/* |
||||
## ownership |
||||
|
||||
### Instruction |
||||
|
||||
Create a function that takes ownership of a string and returns the |
||||
first sub-word in it. It should work for camelCase as well as snake_case |
||||
first_subword(camelCase) returns camel |
||||
first_subword(snake_case) returns snake |
||||
|
||||
And fix the printing expression so the code works |
||||
|
||||
### |
||||
*/ |
||||
|
||||
use ownership::*; |
||||
|
||||
fn main() { |
||||
let s1 = String::from("helloWorld"); |
||||
let s2 = String::from("snake_case"); |
||||
let s3 = String::from("CamelCase"); |
||||
let s4 = String::from("just"); |
||||
|
||||
println!("first_subword({}) = {}", s1.clone(), first_subword(s1)); // Must print first_subword(helloWorld) = hello
|
||||
println!("first_subword({}) = {}", s2.clone(), first_subword(s2)); // Must print first_subword(snake_case) = snake
|
||||
println!("first_subword({}) = {}", s3.clone(), first_subword(s3)); // Must print first_subword(CamelCase) = Camel
|
||||
println!("first_subword({}) = {}", s4.clone(), first_subword(s4)); // Must print first_subword(just) = just
|
||||
} |
||||
|
||||
#[cfg(test)] |
||||
mod tests { |
||||
use super::*; |
||||
|
||||
#[test] |
||||
fn first_subword_test() { |
||||
struct TstString<'a> { |
||||
str: String, |
||||
l: &'a str, |
||||
} |
||||
|
||||
let o_tsts = vec![ |
||||
TstString { |
||||
str: "helloWorld".to_string(), |
||||
l: "hello", |
||||
}, |
||||
TstString { |
||||
str: "how_you".to_string(), |
||||
l: "how", |
||||
}, |
||||
TstString { |
||||
str: "Changeyou".to_string(), |
||||
l: "Changeyou", |
||||
}, |
||||
TstString { |
||||
str: "CamelCase".to_string(), |
||||
l: "Camel", |
||||
}, |
||||
]; |
||||
|
||||
for t in o_tsts.iter() { |
||||
assert_eq!(t.l.to_string(), first_subword(t.str.clone())); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,43 @@
|
||||
## ownership |
||||
|
||||
### Instruction |
||||
|
||||
- Create a function that takes ownership of a string and returns the first sub-word in it |
||||
|
||||
- It should work for `camelCase` as well as `snake_case` |
||||
|
||||
### Expected Function |
||||
|
||||
```rust |
||||
pub fn first_subword(mut s: String) -> String { |
||||
} |
||||
``` |
||||
|
||||
### Usage |
||||
|
||||
Here is a program to test your function |
||||
|
||||
```rust |
||||
fn main() { |
||||
let s1 = String::from("helloWorld"); |
||||
let s2 = String::from("snake_case"); |
||||
let s3 = String::from("CamelCase"); |
||||
let s4 = String::from("just"); |
||||
|
||||
println!("first_subword({}) = {}", s1.clone(), first_subword(s1)); |
||||
println!("first_subword({}) = {}", s2.clone(), first_subword(s2)); |
||||
println!("first_subword({}) = {}", s3.clone(), first_subword(s3)); |
||||
println!("first_subword({}) = {}", s4.clone(), first_subword(s4)); |
||||
} |
||||
``` |
||||
|
||||
And its output |
||||
|
||||
```console |
||||
student@ubuntu:~/[[ROOT]]/test$ cargo run |
||||
first_subword(helloWorld) = hello |
||||
first_subword(snake_case) = snake |
||||
first_subword(CamelCase) = Camel |
||||
first_subword(just) = just |
||||
student@ubuntu:~/[[ROOT]]/test$ |
||||
``` |
Loading…
Reference in new issue