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!
// No hints, there's no tricks, just get used to typing these :)
// I AM NOT DONE
fn main() {
// Booleans (`bool`)
@ -12,7 +11,7 @@ fn main() {
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 {
println!("Good evening!");
}

View file

@ -2,7 +2,6 @@
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)
// I AM NOT DONE
fn main() {
// Characters (`char`)
@ -16,7 +15,7 @@ fn main() {
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
// from a different language than your own, try an emoji!
if your_character.is_alphabetic() {

View file

@ -2,10 +2,9 @@
// Create an array with at least 100 elements in it where the ??? is.
// Execute `rustlings hint primitive_types3` for hints!
// I AM NOT DONE
fn main() {
let a = ???
let a = [1;100]; // 100 elem with 1
if a.len() >= 100 {
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.
// Execute `rustlings hint primitive_types4` for hints!!
// I AM NOT DONE
#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];
let nice_slice = ???
assert_eq!([2, 3, 4], nice_slice)
let nice_slice = &a[1..a.len() - 1];
//https://doc.rust-lang.org/book/ch15-02-deref.html
// 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.
// Execute `rustlings hint primitive_types5` for hints!
// I AM NOT DONE
fn main() {
let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat;
let (name, age) = cat; // tuple decoupling?
println!("{} is {} years old.", name, age);
}

View file

@ -3,9 +3,8 @@
// You can put this right into the `println!` where the ??? is.
// Execute `rustlings hint primitive_types6` for hints!
// I AM NOT DONE
fn main() {
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!
// Execute `rustlings hint strings1` for hints ;)
// I AM NOT DONE
fn main() {
let answer = current_favorite_color();
println!("My current favorite color is {}", answer);
}
fn current_favorite_color() -> String {
"blue"
"blue".to_string()
}

View file

@ -2,11 +2,10 @@
// Make me compile without changing the function signature!
// Execute `rustlings hint strings2` for hints :)
// I AM NOT DONE
fn main() {
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!");
} else {
println!("That is not a color word I know.");

View file

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

View file

@ -2,7 +2,6 @@
// Address all the TODOs to make the tests pass!
// No hints, just do it!
// I AM NOT DONE
#[derive(Debug)]
struct Order {
@ -34,8 +33,7 @@ mod tests {
#[test]
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// let your_order =
let your_order = Order{name: "Hacker in Rust".to_string(), count : 1, ..order_template};
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
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`
// before the parentheses on each line. If you're right, it will compile!
// I AM NOT DONE
fn string_slice(arg: &str) {
println!("{}", arg);
@ -17,14 +16,14 @@ fn string(arg: String) {
}
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());
string_slice("blue");
string("red".to_string());
string(String::from("hi"));
string("rust is fun!".to_owned()); //https://doc.rust-lang.org/std/borrow/trait.ToOwned.html
string("nice weather".into());
string(format!("Interpolation {}", "Station")); //format Marco println String type https://doc.rust-lang.org/std/fmt/
string_slice(&String::from("abc")[0..1]);
string_slice(" hello there ".trim());
string("Happy Monday!".to_string().replace("Mon", "Tues"));
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
}