mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 23:36:30 +00:00
port the first exercise from the old curriculum
This commit is contained in:
parent
729217da2f
commit
6d50965344
1 changed files with 33 additions and 24 deletions
|
@ -1,29 +1,38 @@
|
||||||
fn guess_this() -> i32 {
|
// Welcome to Rustlings! If you're here, that means you've either successfully
|
||||||
let one = 5;
|
// downloaded Rustlings, or are looking at this on GitHub. Either way, let me
|
||||||
let two = 7;
|
// introduce you to one of the most basic elements of Rust:
|
||||||
let three = 3;
|
//
|
||||||
let result = (one + two) / three;
|
// === VARIABLES ===
|
||||||
return result;
|
//
|
||||||
|
// 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() {
|
||||||
|
let x = 5;
|
||||||
|
verify!(0, x, "Number assignment");
|
||||||
|
// ^ ^
|
||||||
|
// | |
|
||||||
|
// What's The variable
|
||||||
|
// in it itself
|
||||||
}
|
}
|
||||||
|
|
||||||
fn simple() -> &'static str {
|
// Did you get all that? The "let" word basically tells us that we now want to
|
||||||
let hello = "Hello World!";
|
// define a variable, and what follows it, the "x" is the name of the variable.
|
||||||
return hello;
|
// 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.
|
||||||
|
// The whole "verify!" deal essentially means that Rustlings is checking if you
|
||||||
mod tests {
|
// solved the exercise correctly. It compares the first argument with the
|
||||||
use super::*;
|
// second, so in this case "0" with "x", where "x" is the _value_ of the variable
|
||||||
|
// we called "x". When you write "x", you pull out the cardboard box labelled "x"
|
||||||
pub fn test_simple() {
|
// and take out what's inside of it.
|
||||||
verify!("Hello World!", simple(), "Simple example");
|
// Speaking of which, what *is* inside of our "x" cardboard box? I don't think it's
|
||||||
}
|
// "0"... do you know? Replace the "0" with the value of the variable we defined.
|
||||||
|
// After that, run "cargo run" in your command line, and see if you put in the
|
||||||
pub fn test_complicated() {
|
// right answer.
|
||||||
verify!(1, guess_this(), "Complicated example");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn exec() {
|
pub fn exec() {
|
||||||
tests::test_simple();
|
exercise_one();
|
||||||
tests::test_complicated();
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue