mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-21 05:32:22 -05:00
strings 3 solution
This commit is contained in:
parent
f574905b8e
commit
613ec23f84
3 changed files with 62 additions and 11 deletions
|
@ -1,16 +1,13 @@
|
|||
fn trim_me(input: &str) -> String {
|
||||
// TODO: Remove whitespace from both ends of a string!
|
||||
???
|
||||
fn trim_me(input: &str) -> &str {
|
||||
// TODO: Remove whitespace from both ends of a string.
|
||||
}
|
||||
|
||||
fn compose_me(input: &str) -> String {
|
||||
// TODO: Add " world!" to the string! There are multiple ways to do this!
|
||||
???
|
||||
// TODO: Add " world!" to the string! There are multiple ways to do this.
|
||||
}
|
||||
|
||||
fn replace_me(input: &str) -> String {
|
||||
// TODO: Replace "cars" in the string with "balloons"!
|
||||
???
|
||||
// TODO: Replace "cars" in the string with "balloons".
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -36,7 +33,13 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn replace_a_string() {
|
||||
assert_eq!(replace_me("I think cars are cool"), "I think balloons are cool");
|
||||
assert_eq!(replace_me("I love to look at cars"), "I love to look at balloons");
|
||||
assert_eq!(
|
||||
replace_me("I think cars are cool"),
|
||||
"I think balloons are cool",
|
||||
);
|
||||
assert_eq!(
|
||||
replace_me("I love to look at cars"),
|
||||
"I love to look at balloons",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -499,7 +499,8 @@ https://doc.rust-lang.org/stable/book/ch15-02-deref.html#implicit-deref-coercion
|
|||
name = "strings3"
|
||||
dir = "09_strings"
|
||||
hint = """
|
||||
There's tons of useful standard library functions for strings. Let's try and use some of them:
|
||||
There are many useful standard library functions for strings. Let's try and use
|
||||
some of them:
|
||||
https://doc.rust-lang.org/std/string/struct.String.html#method.trim
|
||||
|
||||
For the `compose_me` method: You can either use the `format!` macro, or convert
|
||||
|
|
|
@ -1 +1,48 @@
|
|||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||
fn trim_me(input: &str) -> &str {
|
||||
input.trim()
|
||||
}
|
||||
|
||||
fn compose_me(input: &str) -> String {
|
||||
// The macro `format!` has the same syntax as `println!`, but it returns a
|
||||
// string instead of printing it to the terminal.
|
||||
// Equivalent to `input.to_string() + " world!"`
|
||||
format!("{input} world!")
|
||||
}
|
||||
|
||||
fn replace_me(input: &str) -> String {
|
||||
input.replace("cars", "balloons")
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// You can optionally experiment here.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn trim_a_string() {
|
||||
assert_eq!(trim_me("Hello! "), "Hello!");
|
||||
assert_eq!(trim_me(" What's up!"), "What's up!");
|
||||
assert_eq!(trim_me(" Hola! "), "Hola!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compose_a_string() {
|
||||
assert_eq!(compose_me("Hello"), "Hello world!");
|
||||
assert_eq!(compose_me("Goodbye"), "Goodbye world!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replace_a_string() {
|
||||
assert_eq!(
|
||||
replace_me("I think cars are cool"),
|
||||
"I think balloons are cool",
|
||||
);
|
||||
assert_eq!(
|
||||
replace_me("I love to look at cars"),
|
||||
"I love to look at balloons",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue