mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-22 14:02:22 -05:00
replace macros with more general methods
This commit is contained in:
parent
2f1e3bc0c7
commit
69ff4a8b25
3 changed files with 72 additions and 34 deletions
|
@ -1,38 +1,39 @@
|
||||||
// Welcome to Rustlings! If you're here, that means you've either successfully
|
use title;
|
||||||
// downloaded Rustlings, or are looking at this on GitHub. Either way, let me
|
use verify;
|
||||||
// introduce you to one of the most basic elements of Rust:
|
|
||||||
//
|
|
||||||
// === VARIABLES ===
|
|
||||||
//
|
|
||||||
// Variables are essentially little containers that hold, well, something. Think
|
|
||||||
// of them as a little cardboard box that you put stuff into. What can you put
|
|
||||||
// into a virtual cardboard box in Rust? All kinds of stuff, it turns out!
|
|
||||||
// Numbers, words, sequences, and much more. Let's start out simple, though.
|
|
||||||
// Here's our first exercise:
|
|
||||||
|
|
||||||
pub fn exercise_one() {
|
// Variables in Rust are defined using the "let" keyword. Like this:
|
||||||
|
|
||||||
|
fn exercise_one() {
|
||||||
let x = 5;
|
let x = 5;
|
||||||
verify!(0, x, "Number assignment");
|
verify(5, x);
|
||||||
// ^ ^
|
// ^ ^
|
||||||
// | |
|
// | |
|
||||||
// What's The variable
|
// What's The variable
|
||||||
// in it name
|
// in it name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Did you get all that? The "let" word basically tells us that we now want to
|
// Try to replace the "0" with the value of the variable, then run
|
||||||
// define a variable, and what follows it (the "x") is the name of the variable.
|
// "cargo run" and see if it was correct!
|
||||||
// Each variable has a name, like a label you put on your cardboard box so you
|
|
||||||
// don't confuse it with another, similar looking one.
|
// Here's a more complicated example:
|
||||||
// The whole "verify!" deal essentially means that Rustlings is checking if you
|
|
||||||
// solved the exercise correctly. It compares the first argument with the
|
fn guess_me() -> &'static str {
|
||||||
// second, so in this case "0" with "x", where "x" is the *value* of the variable
|
let x = 10;
|
||||||
// we called "x". When you write "x", you pull out the cardboard box labelled "x"
|
if x == 10 {
|
||||||
// and take out what's inside of it.
|
return "Ten!";
|
||||||
// Speaking of which, what *is* inside of our "x" cardboard box? I don't think it's
|
} else {
|
||||||
// "0"... do you know? Replace the "0" with the value of the variable we defined.
|
return "Not ten!";
|
||||||
// After that, run "cargo run" in your command line, and see if you put in the
|
}
|
||||||
// right answer.
|
}
|
||||||
|
|
||||||
|
fn exercise_two() {
|
||||||
|
let result = guess_me();
|
||||||
|
verify("REPLACE ME", result);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn exec() {
|
pub fn exec() {
|
||||||
|
title("Variables: Exercise 1");
|
||||||
exercise_one();
|
exercise_one();
|
||||||
|
title("Variables: Exercise 2");
|
||||||
|
exercise_two();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,21 @@
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! title {
|
||||||
|
($str:expr) => {
|
||||||
|
println!("{} {}", ansi_term::Color::Yellow.bold().paint("RUN"), $str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! verify {
|
macro_rules! verify {
|
||||||
($left:expr, $right:expr, $str:expr) => {
|
($left:expr, $right:expr) => {
|
||||||
use ansi_term::Color::{Green, Red};
|
use ansi_term::Color::{Green, Red};
|
||||||
|
|
||||||
if $left == $right {
|
if $left == $right {
|
||||||
println!("{} {}", Green.bold().paint("PASS"), $str);
|
println!("{} {} == {}", Green.bold().paint("PASS"), $left, $right);
|
||||||
} else {
|
} else {
|
||||||
println!("{} {}", Red.bold().paint("FAIL"), $str);
|
print!("{}", Red.bold().paint("FAIL"));
|
||||||
println!("\tYou submitted {}, but that's not correct!", $left);
|
println!(" You submitted {}, but that's not correct!", $left);
|
||||||
println!("\tPlease correct your code to make this test pass!");
|
println!(" Please correct your code to make this test pass!");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
36
src/main.rs
36
src/main.rs
|
@ -2,11 +2,41 @@
|
||||||
extern crate quicli;
|
extern crate quicli;
|
||||||
extern crate ansi_term;
|
extern crate ansi_term;
|
||||||
|
|
||||||
use ansi_term::Color::Yellow;
|
use ansi_term::Color::{Green, Red, Yellow};
|
||||||
use quicli::prelude::*;
|
use quicli::prelude::*;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
pub fn verify<T: PartialEq + Display>(left: T, right: T) {
|
||||||
|
if left == right {
|
||||||
|
println!("{} {} == {}", Green.bold().paint("PASS"), left, right);
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"{} You submitted {}, but that's not correct!",
|
||||||
|
Red.bold().paint("FAIL"),
|
||||||
|
left
|
||||||
|
);
|
||||||
|
println!(" Please correct your code to make this test pass!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn verify_easy<T: PartialEq + Display>(left: T, right: T) {
|
||||||
|
if left == right {
|
||||||
|
println!("{} {} == {}", Green.bold().paint("PASS"), left, right);
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"{} You submitted {}, but that's not correct!",
|
||||||
|
Red.bold().paint("FAIL"),
|
||||||
|
left
|
||||||
|
);
|
||||||
|
println!(" Expected: {}", right);
|
||||||
|
println!(" Please correct your code to make this test pass!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn title(s: &str) {
|
||||||
|
println!("{} {}", Yellow.bold().paint("RUN"), s);
|
||||||
|
}
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
mod macros;
|
|
||||||
mod about_variables;
|
mod about_variables;
|
||||||
|
|
||||||
#[derive(Debug, StructOpt)]
|
#[derive(Debug, StructOpt)]
|
||||||
|
|
Loading…
Reference in a new issue