mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-02 16:59:18 +00:00
Merge branch 'do_exercise' of https://github.com/Tirthnp/rustlings into do_exercise
This commit is contained in:
commit
2356a8d73b
13
cache/solidity-files-cache.json
vendored
Normal file
13
cache/solidity-files-cache.json
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"_format": "ethers-rs-sol-cache-3",
|
||||
"paths": {
|
||||
"artifacts": "out",
|
||||
"sources": "src",
|
||||
"tests": "test",
|
||||
"scripts": "script",
|
||||
"libraries": [
|
||||
"lib"
|
||||
]
|
||||
},
|
||||
"files": {}
|
||||
}
|
||||
@ -7,7 +7,6 @@
|
||||
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::num::ParseIntError;
|
||||
|
||||
@ -15,7 +14,7 @@ fn main() {
|
||||
let mut tokens = 100;
|
||||
let pretend_user_input = "8";
|
||||
|
||||
let cost = total_cost(pretend_user_input)?;
|
||||
let cost = total_cost(pretend_user_input).unwrap();
|
||||
|
||||
if cost > tokens {
|
||||
println!("You can't afford that many!");
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
// Execute `rustlings hint errors4` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct PositiveNonzeroInteger(u64);
|
||||
@ -17,7 +16,18 @@ enum CreationError {
|
||||
impl PositiveNonzeroInteger {
|
||||
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
|
||||
// Hmm... Why is this always returning an Ok value?
|
||||
Ok(PositiveNonzeroInteger(value as u64))
|
||||
if value > 0
|
||||
{
|
||||
Ok(PositiveNonzeroInteger(value as u64))
|
||||
}
|
||||
else if value == 0
|
||||
{
|
||||
Err(CreationError::Zero)
|
||||
}
|
||||
else
|
||||
{
|
||||
Err(CreationError::Negative)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -22,14 +22,13 @@
|
||||
// Execute `rustlings hint errors5` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::num::ParseIntError;
|
||||
|
||||
// TODO: update the return type of `main()` to make this compile.
|
||||
fn main() -> Result<(), Box<dyn ???>> {
|
||||
fn main() -> Result<(), Box<dyn error::Error >> {
|
||||
let pretend_user_input = "42";
|
||||
let x: i64 = pretend_user_input.parse()?;
|
||||
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::num::ParseIntError;
|
||||
|
||||
@ -25,13 +24,16 @@ impl ParsePosNonzeroError {
|
||||
ParsePosNonzeroError::Creation(err)
|
||||
}
|
||||
// TODO: add another error conversion function here.
|
||||
// fn from_parseint...
|
||||
fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError {
|
||||
ParsePosNonzeroError::ParseInt(err)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
|
||||
// TODO: change this to return an appropriate error instead of panicking
|
||||
// when `parse()` returns an error.
|
||||
let x: i64 = s.parse().unwrap();
|
||||
let x : i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?;
|
||||
|
||||
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
||||
}
|
||||
|
||||
|
||||
@ -6,9 +6,8 @@
|
||||
// Execute `rustlings hint generics1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn main() {
|
||||
let mut shopping_list: Vec<?> = Vec::new();
|
||||
let mut shopping_list: Vec<&'static str> = Vec::new();
|
||||
shopping_list.push("milk");
|
||||
}
|
||||
|
||||
@ -6,14 +6,13 @@
|
||||
// Execute `rustlings hint generics2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Wrapper {
|
||||
value: u32,
|
||||
struct Wrapper<T> {
|
||||
value: T,
|
||||
}
|
||||
|
||||
impl Wrapper {
|
||||
pub fn new(value: u32) -> Self {
|
||||
impl<T> Wrapper<T> {
|
||||
pub fn new(value: T) -> Self {
|
||||
Wrapper { value }
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
// Execute `rustlings hint traits1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
|
||||
trait AppendBar {
|
||||
fn append_bar(self) -> Self;
|
||||
@ -15,6 +15,9 @@ trait AppendBar {
|
||||
|
||||
impl AppendBar for String {
|
||||
// TODO: Implement `AppendBar` for type `String`.
|
||||
fn append_bar(self) -> Self {
|
||||
self + "Bar"
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@ -8,13 +8,19 @@
|
||||
//
|
||||
// Execute `rustlings hint traits2` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
trait AppendBar {
|
||||
fn append_bar(self) -> Self;
|
||||
}
|
||||
|
||||
// TODO: Implement trait `AppendBar` for a vector of strings.
|
||||
impl AppendBar for Vec<String> {
|
||||
fn append_bar(mut self) -> Self {
|
||||
|
||||
self.push(String::from("Bar"));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@ -8,10 +8,12 @@
|
||||
// Execute `rustlings hint traits3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub trait Licensed {
|
||||
fn licensing_info(&self) -> String;
|
||||
fn licensing_info(&self) -> String
|
||||
{
|
||||
String::from("Some information")
|
||||
}
|
||||
}
|
||||
|
||||
struct SomeSoftware {
|
||||
@ -22,7 +24,7 @@ struct OtherSoftware {
|
||||
version_number: String,
|
||||
}
|
||||
|
||||
impl Licensed for SomeSoftware {} // Don't edit this line
|
||||
impl Licensed for SomeSoftware {}// Don't edit this line
|
||||
impl Licensed for OtherSoftware {} // Don't edit this line
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
// Execute `rustlings hint traits4` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub trait Licensed {
|
||||
fn licensing_info(&self) -> String {
|
||||
@ -23,7 +22,7 @@ impl Licensed for SomeSoftware {}
|
||||
impl Licensed for OtherSoftware {}
|
||||
|
||||
// YOU MAY ONLY CHANGE THE NEXT LINE
|
||||
fn compare_license_types(software: ??, software_two: ??) -> bool {
|
||||
fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool {
|
||||
software.licensing_info() == software_two.licensing_info()
|
||||
}
|
||||
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
// Execute `rustlings hint traits5` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub trait SomeTrait {
|
||||
fn some_function(&self) -> bool {
|
||||
@ -30,7 +29,7 @@ impl SomeTrait for OtherStruct {}
|
||||
impl OtherTrait for OtherStruct {}
|
||||
|
||||
// YOU MAY ONLY CHANGE THE NEXT LINE
|
||||
fn some_func(item: ??) -> bool {
|
||||
fn some_func(item: impl SomeTrait + OtherTrait) -> bool {
|
||||
item.some_function() && item.other_function()
|
||||
}
|
||||
|
||||
|
||||
@ -8,9 +8,8 @@
|
||||
// Execute `rustlings hint lifetimes1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn longest(x: &str, y: &str) -> &str {
|
||||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||
if x.len() > y.len() {
|
||||
x
|
||||
} else {
|
||||
|
||||
@ -6,7 +6,6 @@
|
||||
// Execute `rustlings hint lifetimes2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
||||
if x.len() > y.len() {
|
||||
@ -22,6 +21,7 @@ fn main() {
|
||||
{
|
||||
let string2 = String::from("xyz");
|
||||
result = longest(string1.as_str(), string2.as_str());
|
||||
println!("The longest string is '{}'", result);
|
||||
}
|
||||
println!("The longest string is '{}'", result);
|
||||
|
||||
}
|
||||
|
||||
@ -5,11 +5,10 @@
|
||||
// Execute `rustlings hint lifetimes3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Book {
|
||||
author: &str,
|
||||
title: &str,
|
||||
struct Book<'a> {
|
||||
author: &'a str,
|
||||
title: &'a str,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@ -10,12 +10,11 @@
|
||||
// Execute `rustlings hint tests1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn you_can_assert() {
|
||||
assert!();
|
||||
assert!(true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,12 +6,11 @@
|
||||
// Execute `rustlings hint tests2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn you_can_assert_eq() {
|
||||
assert_eq!();
|
||||
assert_eq!(1,1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -7,8 +7,6 @@
|
||||
// Execute `rustlings hint tests3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub fn is_even(num: i32) -> bool {
|
||||
num % 2 == 0
|
||||
}
|
||||
@ -19,11 +17,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn is_true_when_even() {
|
||||
assert!();
|
||||
assert!(is_even(2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_false_when_odd() {
|
||||
assert!();
|
||||
assert!(!is_even(5));
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@
|
||||
// Execute `rustlings hint tests4` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
struct Rectangle {
|
||||
width: i32,
|
||||
@ -30,17 +29,20 @@ mod tests {
|
||||
fn correct_width_and_height() {
|
||||
// This test should check if the rectangle is the size that we pass into its constructor
|
||||
let rect = Rectangle::new(10, 20);
|
||||
assert_eq!(???, 10); // check width
|
||||
assert_eq!(???, 20); // check height
|
||||
assert_eq!(rect.width, 10); // check width
|
||||
assert_eq!(rect.height, 20); // check height
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn negative_width() {
|
||||
// This test should check if program panics when we try to create rectangle with negative width
|
||||
let _rect = Rectangle::new(-10, 10);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
|
||||
fn negative_height() {
|
||||
// This test should check if program panics when we try to create rectangle with negative height
|
||||
let _rect = Rectangle::new(10, -10);
|
||||
|
||||
@ -9,18 +9,18 @@
|
||||
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
|
||||
#[test]
|
||||
fn main() {
|
||||
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||
|
||||
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
|
||||
let mut my_iterable_fav_fruits = my_fav_fruits.iter(); // TODO: Step 1
|
||||
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"custard apple")); // TODO: Step 2
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"peach")); // TODO: Step 3
|
||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
|
||||
assert_eq!(my_iterable_fav_fruits.next(), None); // TODO: Step 4
|
||||
}
|
||||
|
||||
@ -6,7 +6,6 @@
|
||||
// Execute `rustlings hint iterators2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// Step 1.
|
||||
// Complete the `capitalize_first` function.
|
||||
@ -15,7 +14,7 @@ pub fn capitalize_first(input: &str) -> String {
|
||||
let mut c = input.chars();
|
||||
match c.next() {
|
||||
None => String::new(),
|
||||
Some(first) => ???,
|
||||
Some(first) => first.to_uppercase().to_string() + c.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,7 +23,13 @@ pub fn capitalize_first(input: &str) -> String {
|
||||
// Return a vector of strings.
|
||||
// ["hello", "world"] -> ["Hello", "World"]
|
||||
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||
vec![]
|
||||
words.iter().map(|input| capitalize_first(*input)).collect()
|
||||
// let mut x : Vec<String> = vec![];
|
||||
// for word in words.iter()
|
||||
// {
|
||||
// x.push(capitalize_first(word));
|
||||
// }
|
||||
// x
|
||||
}
|
||||
|
||||
// Step 3.
|
||||
@ -32,7 +37,18 @@ pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||
// Return a single string.
|
||||
// ["hello", " ", "world"] -> "Hello World"
|
||||
pub fn capitalize_words_string(words: &[&str]) -> String {
|
||||
String::new()
|
||||
words.iter().map(|input| capitalize_first(*input)).collect()
|
||||
// for word in words.iter()
|
||||
// {
|
||||
// if word == &" "
|
||||
// {
|
||||
// x.push_str(word);
|
||||
// } else {
|
||||
// x.push_str(&capitalize_first(word))
|
||||
// }
|
||||
|
||||
// }
|
||||
//x
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
// Execute `rustlings hint iterators3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum DivisionError {
|
||||
@ -26,23 +25,32 @@ pub struct NotDivisibleError {
|
||||
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||
// Otherwise, return a suitable error.
|
||||
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
||||
todo!();
|
||||
if b == 0 {
|
||||
Err(DivisionError::DivideByZero)
|
||||
} else if a%b == 0 {
|
||||
Ok(a / b)
|
||||
} else {
|
||||
Err(DivisionError::NotDivisible(NotDivisibleError{dividend: a, divisor: b}))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Complete the function and return a value of the correct type so the test
|
||||
// passes.
|
||||
// Desired output: Ok([1, 11, 1426, 3])
|
||||
fn result_with_list() -> () {
|
||||
fn result_with_list() -> Result<Vec<i32>, DivisionError> {
|
||||
let numbers = vec![27, 297, 38502, 81];
|
||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||
let division_results = numbers.into_iter().map(|n| divide(n,27));
|
||||
division_results.collect()
|
||||
}
|
||||
|
||||
// Complete the function and return a value of the correct type so the test
|
||||
// passes.
|
||||
// Desired output: [Ok(1), Ok(11), Ok(1426), Ok(3)]
|
||||
fn list_of_results() -> () {
|
||||
fn list_of_results() -> Vec<Result<i32, DivisionError>> {
|
||||
let numbers = vec![27, 297, 38502, 81];
|
||||
let division_results = numbers.into_iter().map(|n| divide(n, 27));
|
||||
division_results.filter(|x| x.is_ok()).collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -3,9 +3,9 @@
|
||||
// Execute `rustlings hint iterators4` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub fn factorial(num: u64) -> u64 {
|
||||
(1..num+1).into_iter().fold(1, |acc, x| x * acc)
|
||||
// Complete this function to return the factorial of num
|
||||
// Do not use:
|
||||
// - return
|
||||
|
||||
@ -16,15 +16,17 @@
|
||||
//
|
||||
// Execute `rustlings hint quiz3` or use the `hint` watch subcommand for a hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub struct ReportCard {
|
||||
pub grade: f32,
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
pub struct ReportCard<T : Display> {
|
||||
pub grade: T,
|
||||
pub student_name: String,
|
||||
pub student_age: u8,
|
||||
}
|
||||
|
||||
impl ReportCard {
|
||||
impl<T : Display> ReportCard<T> {
|
||||
pub fn print(&self) -> String {
|
||||
format!("{} ({}) - achieved a grade of {}",
|
||||
&self.student_name, &self.student_age, &self.grade)
|
||||
@ -52,7 +54,7 @@ mod tests {
|
||||
fn generate_alphabetic_report_card() {
|
||||
// TODO: Make sure to change the grade here after you finish the exercise.
|
||||
let report_card = ReportCard {
|
||||
grade: 2.1,
|
||||
grade: "A+",
|
||||
student_name: "Gary Plotter".to_string(),
|
||||
student_age: 11,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user