mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
Revert "did some more"
This reverts commit 8239cc2be624d6a6cf040018b56fadbab282c3c9.
This commit is contained in:
parent
c90ae094e9
commit
5e5351139a
@ -11,17 +11,17 @@
|
|||||||
// Execute the command `rustlings hint hashmap1` if you need
|
// Execute the command `rustlings hint hashmap1` if you need
|
||||||
// hints.
|
// hints.
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
fn fruit_basket() -> HashMap<String, u32> {
|
fn fruit_basket() -> HashMap<String, u32> {
|
||||||
let mut basket = HashMap::new();// TODO: declare your hash map here.
|
let mut basket = // TODO: declare your hash map here.
|
||||||
|
|
||||||
// Two bananas are already given for you :)
|
// Two bananas are already given for you :)
|
||||||
basket.insert(String::from("banana"), 2);
|
basket.insert(String::from("banana"), 2);
|
||||||
|
|
||||||
// TODO: Put more fruits in your basket here.
|
// TODO: Put more fruits in your basket here.
|
||||||
basket.insert(String::from("apple"), 2);
|
|
||||||
basket.insert(String::from("mango"), 1);
|
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,6 +12,8 @@
|
|||||||
// Execute the command `rustlings hint hashmap2` if you need
|
// Execute the command `rustlings hint hashmap2` if you need
|
||||||
// hints.
|
// hints.
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Hash, PartialEq, Eq)]
|
#[derive(Hash, PartialEq, Eq)]
|
||||||
@ -36,9 +38,6 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
|||||||
// TODO: Put new fruits if not already present. Note that you
|
// TODO: Put new fruits if not already present. Note that you
|
||||||
// are not allowed to put any type of fruit that's already
|
// are not allowed to put any type of fruit that's already
|
||||||
// present!
|
// present!
|
||||||
if basket.get(&fruit) == None {
|
|
||||||
basket.insert(fruit, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,9 +4,11 @@
|
|||||||
// Make me compile and pass the test!
|
// Make me compile and pass the test!
|
||||||
// Execute the command `rustlings hint vec1` if you need hints.
|
// Execute the command `rustlings hint vec1` if you need hints.
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
|
||||||
let a = [10, 20, 30, 40]; // a plain array
|
let a = [10, 20, 30, 40]; // a plain array
|
||||||
let v = vec![10, 20, 30, 40];// TODO: declare your vector here with the macro for vectors
|
let v = // TODO: declare your vector here with the macro for vectors
|
||||||
|
|
||||||
(a, v)
|
(a, v)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,11 +7,12 @@
|
|||||||
// Execute the command `rustlings hint vec2` if you need
|
// Execute the command `rustlings hint vec2` if you need
|
||||||
// hints.
|
// hints.
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
|
||||||
for i in v.iter_mut() {
|
for i in v.iter_mut() {
|
||||||
// TODO: Fill this up so that each element in the Vec `v` is
|
// TODO: Fill this up so that each element in the Vec `v` is
|
||||||
// multiplied by 2.
|
// multiplied by 2.
|
||||||
*i = *i * 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
// At this point, `v` should be equal to [4, 8, 12, 16, 20].
|
||||||
|
|||||||
@ -5,11 +5,14 @@
|
|||||||
// construct to `Option` that can be used to express error conditions. Let's use it!
|
// construct to `Option` that can be used to express error conditions. Let's use it!
|
||||||
// Execute `rustlings hint errors1` for hints!
|
// Execute `rustlings hint errors1` for hints!
|
||||||
|
|
||||||
pub fn generate_nametag_text(name: String) -> Result<String, String> {
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
pub fn generate_nametag_text(name: String) -> Option<String> {
|
||||||
if name.len() > 0 {
|
if name.len() > 0 {
|
||||||
Ok(format!("Hi! My name is {}", name))
|
Some(format!("Hi! My name is {}", name))
|
||||||
} else {
|
} else {
|
||||||
Err(String::from("`name` was empty; it must be nonempty."))
|
// Empty names aren't allowed.
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,20 +16,14 @@
|
|||||||
// There are at least two ways to implement this that are both correct-- but
|
// There are at least two ways to implement this that are both correct-- but
|
||||||
// one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways.
|
// one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways.
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
let processing_fee = 1;
|
let processing_fee = 1;
|
||||||
let cost_per_item = 5;
|
let cost_per_item = 5;
|
||||||
// method one
|
|
||||||
//let mut qty = item_quantity.parse::<i32>()?;
|
|
||||||
|
|
||||||
// method two
|
|
||||||
let qty = item_quantity.parse::<i32>();
|
let qty = item_quantity.parse::<i32>();
|
||||||
let mut qty = match qty {
|
|
||||||
Ok(number) => number,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(qty * cost_per_item + processing_fee)
|
Ok(qty * cost_per_item + processing_fee)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,18 +4,15 @@
|
|||||||
// Why not? What should we do to fix it?
|
// Why not? What should we do to fix it?
|
||||||
// Execute `rustlings hint errors3` for hints!
|
// Execute `rustlings hint errors3` for hints!
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut tokens = 100;
|
let mut tokens = 100;
|
||||||
let pretend_user_input = "8";
|
let pretend_user_input = "8";
|
||||||
|
|
||||||
let cost = total_cost(pretend_user_input);
|
let cost = total_cost(pretend_user_input)?;
|
||||||
|
|
||||||
let mut cost = match cost {
|
|
||||||
Ok(number) => number,
|
|
||||||
Err(e) => panic!(e),
|
|
||||||
};
|
|
||||||
|
|
||||||
if cost > tokens {
|
if cost > tokens {
|
||||||
println!("You can't afford that many!");
|
println!("You can't afford that many!");
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
// errors4.rs
|
// errors4.rs
|
||||||
// Make this test pass! Execute `rustlings hint errors4` for hints :)
|
// Make this test pass! Execute `rustlings hint errors4` for hints :)
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
|
|
||||||
@ -12,13 +14,6 @@ enum CreationError {
|
|||||||
|
|
||||||
impl PositiveNonzeroInteger {
|
impl PositiveNonzeroInteger {
|
||||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||||
|
|
||||||
if value == 0 {
|
|
||||||
return Err(CreationError::Zero)
|
|
||||||
} else if value < 0 {
|
|
||||||
return Err(CreationError::Negative)
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(PositiveNonzeroInteger(value as u64))
|
Ok(PositiveNonzeroInteger(value as u64))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,12 +4,14 @@
|
|||||||
// It won't compile right now! Why?
|
// It won't compile right now! Why?
|
||||||
// Execute `rustlings hint errors5` for hints!
|
// Execute `rustlings hint errors5` for hints!
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
// TODO: update the return type of `main()` to make this compile.
|
// TODO: update the return type of `main()` to make this compile.
|
||||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
fn main() -> Result<(), ParseIntError> {
|
||||||
let pretend_user_input = "42";
|
let pretend_user_input = "42";
|
||||||
let x: i64 = pretend_user_input.parse()?;
|
let x: i64 = pretend_user_input.parse()?;
|
||||||
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
||||||
|
|||||||
@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
// Make these tests pass! Execute `rustlings hint errors6` for hints :)
|
// Make these tests pass! Execute `rustlings hint errors6` for hints :)
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
|
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
|
||||||
@ -19,13 +21,6 @@ enum ParsePosNonzeroError {
|
|||||||
|
|
||||||
impl ParsePosNonzeroError {
|
impl ParsePosNonzeroError {
|
||||||
// TODO: add another error conversion function here.
|
// TODO: add another error conversion function here.
|
||||||
fn from_creation(err: CreationError) -> ParsePosNonzeroError {
|
|
||||||
ParsePosNonzeroError::Creation(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_parsing(err: ParseIntError) -> ParsePosNonzeroError {
|
|
||||||
ParsePosNonzeroError::ParseInt(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_pos_nonzero(s: &str)
|
fn parse_pos_nonzero(s: &str)
|
||||||
@ -33,12 +28,7 @@ fn parse_pos_nonzero(s: &str)
|
|||||||
{
|
{
|
||||||
// TODO: change this to return an appropriate error instead of panicking
|
// TODO: change this to return an appropriate error instead of panicking
|
||||||
// when `parse()` returns an error.
|
// when `parse()` returns an error.
|
||||||
let x: Result<i64, ParseIntError> = s.parse();
|
let x: i64 = s.parse().unwrap();
|
||||||
let mut x: i64 = match x {
|
|
||||||
Ok(number) => number,
|
|
||||||
Err(e) => return Err(ParsePosNonzeroError::ParseInt(e)),
|
|
||||||
};
|
|
||||||
|
|
||||||
PositiveNonzeroInteger::new(x)
|
PositiveNonzeroInteger::new(x)
|
||||||
.map_err(ParsePosNonzeroError::from_creation)
|
.map_err(ParsePosNonzeroError::from_creation)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint generics1` for hints!
|
// Execute `rustlings hint generics1` for hints!
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut shopping_list: Vec<&str> = Vec::new();
|
let mut shopping_list: Vec<?> = Vec::new();
|
||||||
shopping_list.push("milk");
|
shopping_list.push("milk");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,12 +3,14 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint generics2` for hints!
|
// Execute `rustlings hint generics2` for hints!
|
||||||
|
|
||||||
struct Wrapper<T> {
|
// I AM NOT DONE
|
||||||
value: T,
|
|
||||||
|
struct Wrapper {
|
||||||
|
value: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Wrapper<T> {
|
impl Wrapper {
|
||||||
pub fn new(value: T) -> Self {
|
pub fn new(value: u32) -> Self {
|
||||||
Wrapper { value }
|
Wrapper { value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,13 +10,15 @@
|
|||||||
|
|
||||||
// Execute 'rustlings hint generics3' for hints!
|
// Execute 'rustlings hint generics3' for hints!
|
||||||
|
|
||||||
pub struct ReportCard<T> {
|
// I AM NOT DONE
|
||||||
pub grade: T,
|
|
||||||
|
pub struct ReportCard {
|
||||||
|
pub grade: f32,
|
||||||
pub student_name: String,
|
pub student_name: String,
|
||||||
pub student_age: u8,
|
pub student_age: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: std::fmt::Display> ReportCard<T> {
|
impl ReportCard {
|
||||||
pub fn print(&self) -> String {
|
pub fn print(&self) -> String {
|
||||||
format!("{} ({}) - achieved a grade of {}",
|
format!("{} ({}) - achieved a grade of {}",
|
||||||
&self.student_name, &self.student_age, &self.grade)
|
&self.student_name, &self.student_age, &self.grade)
|
||||||
@ -44,7 +46,7 @@ mod tests {
|
|||||||
fn generate_alphabetic_report_card() {
|
fn generate_alphabetic_report_card() {
|
||||||
// TODO: Make sure to change the grade here after you finish the exercise.
|
// TODO: Make sure to change the grade here after you finish the exercise.
|
||||||
let report_card = ReportCard {
|
let report_card = ReportCard {
|
||||||
grade: "A+",
|
grade: 2.1,
|
||||||
student_name: "Gary Plotter".to_string(),
|
student_name: "Gary Plotter".to_string(),
|
||||||
student_age: 11,
|
student_age: 11,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,11 +3,13 @@
|
|||||||
// 'use' and 'as' keywords. Fix these 'use' statements to make the code compile.
|
// 'use' and 'as' keywords. Fix these 'use' statements to make the code compile.
|
||||||
// Make me compile! Execute `rustlings hint modules2` for hints :)
|
// Make me compile! Execute `rustlings hint modules2` for hints :)
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
mod delicious_snacks {
|
mod delicious_snacks {
|
||||||
|
|
||||||
// TODO: Fix these use statements
|
// TODO: Fix these use statements
|
||||||
pub use self::fruits::PEAR as fruit;
|
use self::fruits::PEAR as ???
|
||||||
pub use self::veggies::CUCUMBER as veggie;
|
use self::veggies::CUCUMBER as ???
|
||||||
|
|
||||||
mod fruits {
|
mod fruits {
|
||||||
pub const PEAR: &'static str = "Pear";
|
pub const PEAR: &'static str = "Pear";
|
||||||
|
|||||||
@ -5,8 +5,10 @@
|
|||||||
// from the std::time module. Bonus style points if you can do it with one line!
|
// from the std::time module. Bonus style points if you can do it with one line!
|
||||||
// Make me compile! Execute `rustlings hint modules3` for hints :)
|
// Make me compile! Execute `rustlings hint modules3` for hints :)
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
// TODO: Complete this use statement
|
// TODO: Complete this use statement
|
||||||
use std::time::{UNIX_EPOCH, SystemTime};
|
use ???
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||||
|
|||||||
@ -7,6 +7,8 @@
|
|||||||
// 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);
|
||||||
}
|
}
|
||||||
@ -15,14 +17,14 @@ fn string(arg: String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
string_slice("blue");
|
???("blue");
|
||||||
string("red".to_string());
|
???("red".to_string());
|
||||||
string(String::from("hi"));
|
???(String::from("hi"));
|
||||||
string("rust is fun!".to_owned());
|
???("rust is fun!".to_owned());
|
||||||
string("nice weather".into());
|
???("nice weather".into());
|
||||||
string(format!("Interpolation {}", "Station"));
|
???(format!("Interpolation {}", "Station"));
|
||||||
string_slice(&String::from("abc")[0..1]);
|
???(&String::from("abc")[0..1]);
|
||||||
string_slice(" hello there ".trim());
|
???(" hello there ".trim());
|
||||||
string("Happy Monday!".to_string().replace("Mon", "Tues"));
|
???("Happy Monday!".to_string().replace("Mon", "Tues"));
|
||||||
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
???("mY sHiFt KeY iS sTiCkY".to_lowercase());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,11 +2,13 @@
|
|||||||
// 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 {
|
||||||
String::from("blue")
|
"blue"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,11 @@
|
|||||||
// 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.");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user