Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions exercises/enums/enums1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
Quit,
Echo,
Move,
ChangeColor
}

fn main() {
Expand Down
4 changes: 4 additions & 0 deletions exercises/enums/enums2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#[derive(Debug)]
enum Message {
// TODO: define the different variants used below
Move {x:i32, y:i32},
Echo(String),
ChangeColor(i32, i32, i32),
Quit
}

impl Message {
Expand Down
10 changes: 10 additions & 0 deletions exercises/enums/enums3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

enum Message {
// TODO: implement the message variant types based on their usage below
ChangeColor((u8, u8, u8)),
Echo(String),
Move(Point),
Quit
}

struct Point {
Expand Down Expand Up @@ -38,6 +42,12 @@ impl State {

fn process(&mut self, message: Message) {
// TODO: create a match expression to process the different message variants
match message {
Message::ChangeColor(color_tup) => self.change_color(color_tup),
Message::Echo(string) => self.echo(string),
Message::Move(pos) => self.move_position(pos),
Message::Quit => self.quit()
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions exercises/error_handling/errors1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

// I AM NOT DONE

pub fn generate_nametag_text(name: String) -> Option<String> {
pub fn generate_nametag_text(name: String) -> Result<String, String> {
if name.is_empty() {
// Empty names aren't allowed.
None
Err("`name` was empty; it must be nonempty.".into())
} else {
Some(format!("Hi! My name is {}", name))
Ok(format!("Hi! My name is {}", name))
}
}

Expand Down
6 changes: 4 additions & 2 deletions exercises/error_handling/errors2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>();

Ok(qty * cost_per_item + processing_fee)
match qty {
Ok(value) => Ok(value * cost_per_item + processing_fee),
_ => qty
}
}

#[cfg(test)]
Expand Down
3 changes: 2 additions & 1 deletion exercises/error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::num::ParseIntError;

fn main() {
fn main()-> Result<(), ParseIntError>{
let mut tokens = 100;
let pretend_user_input = "8";

Expand All @@ -20,6 +20,7 @@ fn main() {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}
Ok(())
}

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
Expand Down
5 changes: 5 additions & 0 deletions exercises/error_handling/errors4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ enum CreationError {
impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
// Hmm...? Why is this only returning an Ok value?
if value < 0 {
return Err(CreationError::Negative)
}else if value == 0 {
return Err(CreationError::Zero)
}
Ok(PositiveNonzeroInteger(value as u64))
}
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/error_handling/errors5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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)?);
Expand Down
5 changes: 4 additions & 1 deletion exercises/error_handling/errors6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ impl ParsePosNonzeroError {
}
// 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)
}
Expand Down
3 changes: 3 additions & 0 deletions exercises/functions/functions1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

// I AM NOT DONE

fn call_me() {
print!("hello world!");
}
fn main() {
call_me();
}
2 changes: 1 addition & 1 deletion exercises/functions/functions2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn main() {
call_me(3);
}

fn call_me(num:) {
fn call_me(num:u32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
Expand Down
4 changes: 1 addition & 3 deletions exercises/functions/functions3.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// functions3.rs
// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

fn main() {
call_me();
call_me(5);
}

fn call_me(num: u32) {
Expand Down
2 changes: 1 addition & 1 deletion exercises/functions/functions4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
println!("Your sale price is {}", sale_price(original_price));
}

fn sale_price(price: i32) -> {
fn sale_price(price: i32) -> i32{
if is_even(price) {
price - 10
} else {
Expand Down
2 changes: 1 addition & 1 deletion exercises/functions/functions5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ fn main() {
}

fn square(num: i32) -> i32 {
num * num;
num * num
}
2 changes: 0 additions & 2 deletions exercises/intro/intro1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
// when you change one of the lines below! Try adding a `println!` line, or try changing
// what it outputs in your terminal. Try removing a semicolon and see what happens!

// I AM NOT DONE

fn main() {
println!("Hello and");
println!(r#" welcome to... "#);
Expand Down
2 changes: 1 addition & 1 deletion exercises/intro/intro2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
// I AM NOT DONE

fn main() {
println!("Hello {}!");
println!("Hello {}!","world");
}
2 changes: 1 addition & 1 deletion exercises/move_semantics/move_semantics1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
fn main() {
let vec0 = Vec::new();

let vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(vec0);

println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);

Expand Down
6 changes: 3 additions & 3 deletions exercises/move_semantics/move_semantics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
fn main() {
let vec0 = Vec::new();

let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec(&vec0);

// Do not change the following line!
println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0);
Expand All @@ -17,8 +17,8 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}

fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;
fn fill_vec(vec: &Vec<i32>) -> Vec<i32> {
let mut vec = vec.to_vec();

vec.push(22);
vec.push(44);
Expand Down
2 changes: 1 addition & 1 deletion exercises/move_semantics/move_semantics3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);
}

fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(22);
vec.push(44);
vec.push(66);
Expand Down
6 changes: 2 additions & 4 deletions exercises/move_semantics/move_semantics4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
// I AM NOT DONE

fn main() {
let vec0 = Vec::new();

let mut vec1 = fill_vec(vec0);
let mut vec1 = fill_vec();

println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1);

Expand All @@ -20,7 +18,7 @@ fn main() {

// `fill_vec()` no longer takes `vec: Vec<i32>` as argument
fn fill_vec() -> Vec<i32> {
let mut vec = vec;
let mut vec = Vec::new();

vec.push(22);
vec.push(44);
Expand Down
2 changes: 1 addition & 1 deletion exercises/move_semantics/move_semantics5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
fn main() {
let mut x = 100;
let y = &mut x;
let z = &mut x;
*y += 100;
let z = &mut x;
*z += 1000;
assert_eq!(x, 1200);
}
10 changes: 5 additions & 5 deletions exercises/move_semantics/move_semantics6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
fn main() {
let data = "Rust is great!".to_string();

get_char(data);
get_char(&data);

string_uppercase(&data);
string_uppercase(data);
}

// Should not take ownership
fn get_char(data: String) -> char {
fn get_char(data: &str) -> char {
data.chars().last().unwrap()
}

// Should take ownership
fn string_uppercase(mut data: &String) {
data = &data.to_uppercase();
fn string_uppercase(mut data: String) {
data = data.to_uppercase();

println!("{}", data);
}
4 changes: 2 additions & 2 deletions exercises/standard_library_types/arc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ use std::thread;

fn main() {
let numbers: Vec<_> = (0..100u32).collect();
let shared_numbers = // TODO
let shared_numbers = Arc::new(numbers); // TODO
let mut joinhandles = Vec::new();

for offset in 0..8 {
let child_numbers = // TODO
let child_numbers = shared_numbers.clone();// TODO
joinhandles.push(thread::spawn(move || {
let sum: u32 = child_numbers.iter().filter(|n| *n % 8 == offset).sum();
println!("Sum of offset {} is {}", offset, sum);
Expand Down
2 changes: 1 addition & 1 deletion exercises/strings/strings1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ fn main() {
println!("My current favorite color is {}", answer);
}

fn current_favorite_color() -> String {
fn current_favorite_color() -> &'static str {
"blue"
}
2 changes: 1 addition & 1 deletion exercises/strings/strings2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ fn main() {
}
}

fn is_a_color_word(attempt: &str) -> bool {
fn is_a_color_word(attempt: String) -> bool {
attempt == "green" || attempt == "blue" || attempt == "red"
}
6 changes: 3 additions & 3 deletions exercises/strings/strings3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

fn trim_me(input: &str) -> String {
// TODO: Remove whitespace from both ends of a string!
???
input.to_string().trim().to_string()
}

fn compose_me(input: &str) -> String {
// TODO: Add " world!" to the string! There's multiple ways to do this!
???
input.to_string() + " world!"
}

fn replace_me(input: &str) -> String {
// TODO: Replace "cars" in the string with "balloons"!
???
input.to_string().replace("cars", "balloons")
}

#[cfg(test)]
Expand Down
20 changes: 10 additions & 10 deletions exercises/strings/strings4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,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());
string_slice("nice weather".into());
string(format!("Interpolation {}", "Station"));
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());
}
Loading