diff --git a/README.md b/README.md index 2cd88105..9f163415 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,10 @@ If you need more help or would like to compare solutions, you can ask in [#rust [Relevant chapter in The Rust Programming Language](https://doc.rust-lang.org/stable/book/variable-bindings.html) -- ["variables1.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21%0A%0Afn+main%28%29+%7B%0A++++x+%3D+5%3B%0A++++println%21%28%22x+has+the+value+%7B%7D%22%2C+x%29%3B%0A%7D%0A) -- ["variables2.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21%0A%0Afn+main%28%29+%7B%0A++++let+x%3B%0A++++if+x+%3D%3D+10+%7B%0A++++++++println%21%28%22Ten%21%22%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22Not+ten%21%22%29%3B%0A++++%7D%0A%7D%0A) -- ["variables3.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21%0A%0Afn+main%28%29+%7B%0A++++let+x+%3D+3%3B%0A++++println%21%28%22Number+%7B%7D%22%2C+x%29%3B%0A++++x+%3D+5%3B%0A++++println%21%28%22Number+%7B%7D%22%2C+x%29%3B%0A%7D%0A) -- ["variables4.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21%0A%0Afn+main%28%29+%7B%0A++++let+x%3A+i32%3B%0A++++println%21%28%22Number+%7B%7D%22%2C+x%29%3B%0A%7D%0A) +- ["variables1.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21+Scroll+down+for+hints+%3A%29%0A%0Afn+main%28%29+%7B%0A++++x+%3D+5%3B%0A++++println%21%28%22x+has+the+value+%7B%7D%22%2C+x%29%3B%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Hint%3A+The+declaration+on+line+4+is+missing+a+keyword+that+is+needed+in+Rust%0A%2F%2F+to+create+a+new+variable+binding.%0A) +- ["variables2.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21+Scroll+down+for+hints+%3A%29%0A%0Afn+main%28%29+%7B%0A++++let+x%3B%0A++++if+x+%3D%3D+10+%7B%0A++++++++println%21%28%22Ten%21%22%29%3B%0A++++%7D+else+%7B%0A++++++++println%21%28%22Not+ten%21%22%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+The+compiler+message+is+saying+that+Rust+cannot+infer+the+type+that+the%0A%2F%2F+variable+binding+%60x%60+has+with+what+is+given+here.%0A%2F%2F+What+happens+if+you+annotate+line+4+with+a+type+annotation%3F%0A%2F%2F+What+if+you+give+x+a+value%3F%0A%2F%2F+What+if+you+do+both%3F%0A%2F%2F+What+type+should+x+be%2C+anyway%3F%0A%2F%2F+What+if+x+is+the+same+type+as+10%3F+What+if+it%27s+a+different+type%3F%0A) +- ["variables3.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21+Scroll+down+for+hints+%3A%29%0A%0Afn+main%28%29+%7B%0A++++let+x+%3D+3%3B%0A++++println%21%28%22Number+%7B%7D%22%2C+x%29%3B%0A++++x+%3D+5%3B%0A++++println%21%28%22Number+%7B%7D%22%2C+x%29%3B%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+In+Rust%2C+variable+bindings+are+immutable+by+default.+But+here+we%27re+trying%0A%2F%2F+to+reassign+a+different+value+to+x%21+There%27s+a+keyword+we+can+use+to+make%0A%2F%2F+a+variable+binding+mutable+instead.%0A) +- ["variables4.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21+Scroll+down+for+hints+%3A%29%0A%0Afn+main%28%29+%7B%0A++++let+x%3A+i32%3B%0A++++println%21%28%22Number+%7B%7D%22%2C+x%29%3B%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Oops%21+In+this+exercise%2C+we+have+a+variable+binding+that+we%27ve+created+on%0A%2F%2F+line+4%2C+and+we%27re+trying+to+use+it+on+line+5%2C+but+we+haven%27t+given+it+a%0A%2F%2F+value.+We+can%27t+print+out+something+that+isn%27t+there%3B+try+giving+x+a+value%21%0A%2F%2F+This+is+an+error+that+can+cause+bugs+that%27s+very+easy+to+make+in+any%0A%2F%2F+programming+language+--+thankfully+the+Rust+compiler+has+caught+this+for+us%21%0A) ### Uncategorized diff --git a/variables/variables1.rs b/variables/variables1.rs index 3035bfae..e0ad5de8 100644 --- a/variables/variables1.rs +++ b/variables/variables1.rs @@ -1,6 +1,41 @@ -// Make me compile! +// Make me compile! Scroll down for hints :) fn main() { x = 5; println!("x has the value {}", x); } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// Hint: The declaration on line 4 is missing a keyword that is needed in Rust +// to create a new variable binding. diff --git a/variables/variables2.rs b/variables/variables2.rs index 93684885..ca024387 100644 --- a/variables/variables2.rs +++ b/variables/variables2.rs @@ -1,4 +1,4 @@ -// Make me compile! +// Make me compile! Scroll down for hints :) fn main() { let x; @@ -8,3 +8,39 @@ fn main() { println!("Not ten!"); } } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// The compiler message is saying that Rust cannot infer the type that the +// variable binding `x` has with what is given here. +// What happens if you annotate line 4 with a type annotation? +// What if you give x a value? +// What if you do both? +// What type should x be, anyway? +// What if x is the same type as 10? What if it's a different type? diff --git a/variables/variables3.rs b/variables/variables3.rs index cadb40b0..ec55a951 100644 --- a/variables/variables3.rs +++ b/variables/variables3.rs @@ -1,4 +1,4 @@ -// Make me compile! +// Make me compile! Scroll down for hints :) fn main() { let x = 3; @@ -6,3 +6,37 @@ fn main() { x = 5; println!("Number {}", x); } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// In Rust, variable bindings are immutable by default. But here we're trying +// to reassign a different value to x! There's a keyword we can use to make +// a variable binding mutable instead. diff --git a/variables/variables4.rs b/variables/variables4.rs index c9a63f5d..3f7f937d 100644 --- a/variables/variables4.rs +++ b/variables/variables4.rs @@ -1,6 +1,44 @@ -// Make me compile! +// Make me compile! Scroll down for hints :) fn main() { let x: i32; println!("Number {}", x); } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// Oops! In this exercise, we have a variable binding that we've created on +// line 4, and we're trying to use it on line 5, but we haven't given it a +// value. We can't print out something that isn't there; try giving x a value! +// This is an error that can cause bugs that's very easy to make in any +// programming language -- thankfully the Rust compiler has caught this for us!