mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-09 20:29:18 +00:00
update:update the enums and strings
This commit is contained in:
parent
1807ba8439
commit
aad6b7ad2a
@ -2,11 +2,13 @@
|
|||||||
//
|
//
|
||||||
// No hints this time! ;)
|
// No hints this time! ;)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: define a few types of messages as used below
|
Quit,
|
||||||
|
Echo,
|
||||||
|
Move,
|
||||||
|
ChangeColor,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@ -3,11 +3,13 @@
|
|||||||
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint enums2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: define the different variants used below
|
// TODO: define the different variants used below
|
||||||
|
Move { x: i32, y: i32 },
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor(i32, i32, i32),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|||||||
@ -5,10 +5,12 @@
|
|||||||
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint enums3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: implement the message variant types based on their usage below
|
// TODO: implement the message variant types based on their usage below
|
||||||
|
ChangeColor(u8, u8, u8),
|
||||||
|
Move(Point),
|
||||||
|
Quit,
|
||||||
|
Echo(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
@ -20,7 +22,7 @@ struct State {
|
|||||||
color: (u8, u8, u8),
|
color: (u8, u8, u8),
|
||||||
position: Point,
|
position: Point,
|
||||||
quit: bool,
|
quit: bool,
|
||||||
message: String
|
message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
@ -32,7 +34,9 @@ impl State {
|
|||||||
self.quit = true;
|
self.quit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn echo(&mut self, s: String) { self.message = s }
|
fn echo(&mut self, s: String) {
|
||||||
|
self.message = s
|
||||||
|
}
|
||||||
|
|
||||||
fn move_position(&mut self, p: Point) {
|
fn move_position(&mut self, p: Point) {
|
||||||
self.position = p;
|
self.position = p;
|
||||||
@ -43,6 +47,12 @@ impl State {
|
|||||||
// variants
|
// variants
|
||||||
// Remember: When passing a tuple as a function argument, you'll need
|
// Remember: When passing a tuple as a function argument, you'll need
|
||||||
// extra parentheses: fn function((t, u, p, l, e))
|
// extra parentheses: fn function((t, u, p, l, e))
|
||||||
|
match message {
|
||||||
|
Message::ChangeColor(a, b, c) => self.color = (a, b, c),
|
||||||
|
Message::Move(point) => self.position = point,
|
||||||
|
Message::Quit => self.quit = true,
|
||||||
|
Message::Echo(message) => self.message = message,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,13 +5,12 @@
|
|||||||
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let answer = current_favorite_color();
|
let answer = current_favorite_color();
|
||||||
println!("My current favorite color is {}", answer);
|
println!("My current favorite color is {}", answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_favorite_color() -> String {
|
fn current_favorite_color() -> String {
|
||||||
"blue"
|
// "blue".to_string()
|
||||||
|
String::from("blue")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,11 +5,9 @@
|
|||||||
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let word = String::from("green"); // Try not changing this line :)
|
let word = String::from("green"); // Try not changing this line :)
|
||||||
if is_a_color_word(word) {
|
if is_a_color_word(&word) {
|
||||||
println!("That is a color word I know!");
|
println!("That is a color word I know!");
|
||||||
} else {
|
} else {
|
||||||
println!("That is not a color word I know.");
|
println!("That is not a color word I know.");
|
||||||
|
|||||||
@ -3,21 +3,22 @@
|
|||||||
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn trim_me(input: &str) -> String {
|
fn trim_me(input: &str) -> String {
|
||||||
// TODO: Remove whitespace from both ends of a string!
|
// TODO: Remove whitespace from both ends of a string!
|
||||||
???
|
input.to_string().trim().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compose_me(input: &str) -> String {
|
fn compose_me(input: &str) -> String {
|
||||||
// TODO: Add " world!" to the string! There's multiple ways to do this!
|
// TODO: Add " world!" to the string! There's multiple ways to do this!
|
||||||
???
|
let mut input = String::from(input);
|
||||||
|
input.push_str(" world!");
|
||||||
|
input
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_me(input: &str) -> String {
|
fn replace_me(input: &str) -> String {
|
||||||
// TODO: Replace "cars" in the string with "balloons"!
|
// TODO: Replace "cars" in the string with "balloons"!
|
||||||
???
|
let mut input = String::from(input);
|
||||||
|
input.replace("cars", "balloons")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -39,7 +40,13 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn replace_a_string() {
|
fn replace_a_string() {
|
||||||
assert_eq!(replace_me("I think cars are cool"), "I think balloons are cool");
|
assert_eq!(
|
||||||
assert_eq!(replace_me("I love to look at cars"), "I love to look at balloons");
|
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"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,8 +7,6 @@
|
|||||||
//
|
//
|
||||||
// No hints this time!
|
// No hints this time!
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn string_slice(arg: &str) {
|
fn string_slice(arg: &str) {
|
||||||
println!("{}", arg);
|
println!("{}", arg);
|
||||||
}
|
}
|
||||||
@ -17,14 +15,14 @@ fn string(arg: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
???("blue");
|
string_slice("blue");
|
||||||
???("red".to_string());
|
string("red".to_string());
|
||||||
???(String::from("hi"));
|
string(String::from("hi"));
|
||||||
???("rust is fun!".to_owned());
|
string("rust is fun!".to_owned());
|
||||||
???("nice weather".into());
|
string_slice("nice weather".into());
|
||||||
???(format!("Interpolation {}", "Station"));
|
string(format!("Interpolation {}", "Station"));
|
||||||
???(&String::from("abc")[0..1]);
|
string_slice(&String::from("abc")[0..1]);
|
||||||
???(" hello there ".trim());
|
string_slice(" hello there ".trim());
|
||||||
???("Happy Monday!".to_string().replace("Mon", "Tues"));
|
string("Happy Monday!".to_string().replace("Mon", "Tues"));
|
||||||
???("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user