diff options
| author | olivia <olivia@fastmail.com> | 2018-11-09 20:31:14 +0100 |
|---|---|---|
| committer | olivia <olivia@fastmail.com> | 2018-11-09 20:31:14 +0100 |
| commit | f7846af7ac388652a6f80a2bbce926ba8f053062 (patch) | |
| tree | 954ee36257047ac612654c5f35e18ed27deda97f /exercises/strings | |
| parent | 850a13e9133fedb2fce27884902e0aab94da9692 (diff) | |
right let's try this one again
Diffstat (limited to 'exercises/strings')
| -rwxr-xr-x | exercises/strings/strings1.rs | 46 | ||||
| -rwxr-xr-x | exercises/strings/strings2.rs | 44 | ||||
| -rwxr-xr-x | exercises/strings/strings3.rs | 21 |
3 files changed, 111 insertions, 0 deletions
diff --git a/exercises/strings/strings1.rs b/exercises/strings/strings1.rs new file mode 100755 index 0000000..2e5088f --- /dev/null +++ b/exercises/strings/strings1.rs @@ -0,0 +1,46 @@ +// strings1.rs +// Make me compile without changing the function signature! Scroll down for hints :) + +fn main() { + let answer = current_favorite_color(); + println!("My current favorite color is {}", answer); +} + +fn current_favorite_color() -> String { + "blue" +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// The `current_favorite_color` function is currently returning a string slice with the `'static` +// lifetime. We know this because the data of the string lives in our code itself -- it doesn't +// come from a file or user input or another program -- so it will live as long as our program +// lives. But it is still a string slice. There's one way to create a `String` by converting a +// string slice covered in the Strings chapter of the book, and another way that uses the `From` +// trait. diff --git a/exercises/strings/strings2.rs b/exercises/strings/strings2.rs new file mode 100755 index 0000000..c77e16f --- /dev/null +++ b/exercises/strings/strings2.rs @@ -0,0 +1,44 @@ +// strings2.rs +// Make me compile without changing the function signature! Scroll down for hints :) + +fn main() { + let word = String::from("green"); // Try not changing this line :) + if is_a_color_word(word) { + println!("That is a color word I know!"); + } else { + println!("That is not a color word I know."); + } +} + +fn is_a_color_word(attempt: &str) -> bool { + attempt == "green" || attempt == "blue" || attempt == "red" +} + + + + + + + + + + + + + + + + + + + + + + + + + + +// Yes, it would be really easy to fix this by just changing the value bound to `word` to be a +// string slice instead of a `String`, wouldn't it?? There is a way to add one character to line +// 6, though, that will coerce the `String` into a string slice. diff --git a/exercises/strings/strings3.rs b/exercises/strings/strings3.rs new file mode 100755 index 0000000..b6f6a1e --- /dev/null +++ b/exercises/strings/strings3.rs @@ -0,0 +1,21 @@ +// strings3.rs +// Ok, here are a bunch of values-- some are `Strings`, some are `&strs`. Your +// task is to call one of these two functions on each value depending on what +// you think each value is. That is, add either `string_slice` or `string` +// before the parentheses on each line. If you're right, it will compile! + +fn string_slice(arg: &str) { println!("{}", arg); } +fn string(arg: String) { println!("{}", arg); } + +fn main() { + ("blue"); + ("red".to_string()); + (String::from("hi")); + ("rust is fun!".to_owned()); + ("nice weather".into()); + (format!("Interpolation {}", "Station")); + (&String::from("abc")[0..1]); + (" hello there ".trim()); + ("Happy Monday!".to_string().replace("Mon", "Tues")); + ("mY sHiFt KeY iS sTiCkY".to_lowercase()); +} |
