string, primitive type, struct

This commit is contained in:
zhihanz 2020-05-03 21:31:41 -07:00
parent cd1dda27af
commit 9c559d9965
11 changed files with 29 additions and 43 deletions

View File

@ -2,7 +2,6 @@
// Fill in the rest of the line that has code missing! // Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :) // No hints, there's no tricks, just get used to typing these :)
// I AM NOT DONE
fn main() { fn main() {
// Booleans (`bool`) // Booleans (`bool`)
@ -12,7 +11,7 @@ fn main() {
println!("Good morning!"); println!("Good morning!");
} }
let // Finish the rest of this line like the example! Or make it be false! let is_evening = false;// Finish the rest of this line like the example! Or make it be false!
if is_evening { if is_evening {
println!("Good evening!"); println!("Good evening!");
} }

View File

@ -2,7 +2,6 @@
// Fill in the rest of the line that has code missing! // Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :) // No hints, there's no tricks, just get used to typing these :)
// I AM NOT DONE
fn main() { fn main() {
// Characters (`char`) // Characters (`char`)
@ -16,7 +15,7 @@ fn main() {
println!("Neither alphabetic nor numeric!"); println!("Neither alphabetic nor numeric!");
} }
let // Finish this line like the example! What's your favorite character? let your_character = '3';// Finish this line like the example! What's your favorite character?
// Try a letter, try a number, try a special character, try a character // Try a letter, try a number, try a special character, try a character
// from a different language than your own, try an emoji! // from a different language than your own, try an emoji!
if your_character.is_alphabetic() { if your_character.is_alphabetic() {

View File

@ -2,10 +2,9 @@
// Create an array with at least 100 elements in it where the ??? is. // Create an array with at least 100 elements in it where the ??? is.
// Execute `rustlings hint primitive_types3` for hints! // Execute `rustlings hint primitive_types3` for hints!
// I AM NOT DONE
fn main() { fn main() {
let a = ??? let a = [1;100]; // 100 elem with 1
if a.len() >= 100 { if a.len() >= 100 {
println!("Wow, that's a big array!"); println!("Wow, that's a big array!");

View File

@ -2,13 +2,13 @@
// Get a slice out of Array a where the ??? is so that the test passes. // Get a slice out of Array a where the ??? is so that the test passes.
// Execute `rustlings hint primitive_types4` for hints!! // Execute `rustlings hint primitive_types4` for hints!!
// I AM NOT DONE
#[test] #[test]
fn slice_out_of_array() { fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5]; let a = [1, 2, 3, 4, 5];
let nice_slice = ??? let nice_slice = &a[1..a.len() - 1];
//https://doc.rust-lang.org/book/ch15-02-deref.html
assert_eq!([2, 3, 4], nice_slice) // implement a deref trait
assert_eq!([2, 3, 4], *nice_slice)
} }

View File

@ -2,11 +2,10 @@
// Destructure the `cat` tuple so that the println will work. // Destructure the `cat` tuple so that the println will work.
// Execute `rustlings hint primitive_types5` for hints! // Execute `rustlings hint primitive_types5` for hints!
// I AM NOT DONE
fn main() { fn main() {
let cat = ("Furry McFurson", 3.5); let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat; let (name, age) = cat; // tuple decoupling?
println!("{} is {} years old.", name, age); println!("{} is {} years old.", name, age);
} }

View File

@ -3,9 +3,8 @@
// You can put this right into the `println!` where the ??? is. // You can put this right into the `println!` where the ??? is.
// Execute `rustlings hint primitive_types6` for hints! // Execute `rustlings hint primitive_types6` for hints!
// I AM NOT DONE
fn main() { fn main() {
let numbers = (1, 2, 3); let numbers = (1, 2, 3);
println!("The second number is {}", ???); println!("The second number is {}", numbers.2);
} }

View File

@ -2,13 +2,11 @@
// Make me compile without changing the function signature! // Make me compile without changing the function signature!
// Execute `rustlings hint strings1` for hints ;) // Execute `rustlings hint strings1` for hints ;)
// 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()
} }

View File

@ -2,11 +2,10 @@
// Make me compile without changing the function signature! // Make me compile without changing the function signature!
// Execute `rustlings hint strings2` for hints :) // Execute `rustlings hint strings2` for hints :)
// 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.");

View File

@ -1,13 +1,13 @@
// structs1.rs // structs1.rs
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// I AM NOT DONE
struct ColorClassicStruct { struct ColorClassicStruct<'a> {
// TODO: Something goes here name: &'a str,
hex: &'a str,
} }
struct ColorTupleStruct(/* TODO: Something goes here */); struct ColorTupleStruct<'a>(&'a str, &'a str);
#[derive(Debug)] #[derive(Debug)]
struct UnitStruct; struct UnitStruct;
@ -18,8 +18,7 @@ mod tests {
#[test] #[test]
fn classic_c_structs() { fn classic_c_structs() {
// TODO: Instantiate a classic c struct! let green = ColorClassicStruct{name: "green", hex: "#00FF00"};
// let green =
assert_eq!(green.name, "green"); assert_eq!(green.name, "green");
assert_eq!(green.hex, "#00FF00"); assert_eq!(green.hex, "#00FF00");
@ -27,8 +26,7 @@ mod tests {
#[test] #[test]
fn tuple_structs() { fn tuple_structs() {
// TODO: Instantiate a tuple struct! let green = ColorTupleStruct("green", "#00FF00");
// let green =
assert_eq!(green.0, "green"); assert_eq!(green.0, "green");
assert_eq!(green.1, "#00FF00"); assert_eq!(green.1, "#00FF00");
@ -36,8 +34,7 @@ mod tests {
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit struct! let unit_struct = UnitStruct{};
// let unit_struct =
let message = format!("{:?}s are fun!", unit_struct); let message = format!("{:?}s are fun!", unit_struct);
assert_eq!(message, "UnitStructs are fun!"); assert_eq!(message, "UnitStructs are fun!");

View File

@ -2,7 +2,6 @@
// Address all the TODOs to make the tests pass! // Address all the TODOs to make the tests pass!
// No hints, just do it! // No hints, just do it!
// I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Order { struct Order {
@ -34,8 +33,7 @@ mod tests {
#[test] #[test]
fn your_order() { fn your_order() {
let order_template = create_order_template(); let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above! let your_order = Order{name: "Hacker in Rust".to_string(), count : 1, ..order_template};
// let your_order =
assert_eq!(your_order.name, "Hacker in Rust"); assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year); assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone); assert_eq!(your_order.made_by_phone, order_template.made_by_phone);

View File

@ -7,7 +7,6 @@
// you think each value is. That is, add either `string_slice` or `string` // 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! // before the parentheses on each line. If you're right, it will compile!
// I AM NOT DONE
fn string_slice(arg: &str) { fn string_slice(arg: &str) {
println!("{}", arg); println!("{}", arg);
@ -17,14 +16,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()); //https://doc.rust-lang.org/std/borrow/trait.ToOwned.html
("nice weather".into()); string("nice weather".into());
(format!("Interpolation {}", "Station")); string(format!("Interpolation {}", "Station")); //format Marco println String type https://doc.rust-lang.org/std/fmt/
(&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()); //change thevalue and it will create a new String https://doc.rust-lang.org/std/primitive.str.html#method.to_lowercase
} }