diff options
Diffstat (limited to 'old_curriculum/variables')
| -rw-r--r-- | old_curriculum/variables/variables1.rs | 42 | ||||
| -rw-r--r-- | old_curriculum/variables/variables2.rs | 47 | ||||
| -rw-r--r-- | old_curriculum/variables/variables3.rs | 43 | ||||
| -rw-r--r-- | old_curriculum/variables/variables4.rs | 45 |
4 files changed, 177 insertions, 0 deletions
diff --git a/old_curriculum/variables/variables1.rs b/old_curriculum/variables/variables1.rs new file mode 100644 index 0000000..1cdd270 --- /dev/null +++ b/old_curriculum/variables/variables1.rs @@ -0,0 +1,42 @@ +// variables1.rs +// Make me compile! Scroll down for hints :) + +fn main() { + x = 5; + println!("x has the value {}", x); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +// Hint: The declaration on line 5 is missing a keyword that is needed in Rust +// to create a new variable binding. diff --git a/old_curriculum/variables/variables2.rs b/old_curriculum/variables/variables2.rs new file mode 100644 index 0000000..a0b4a37 --- /dev/null +++ b/old_curriculum/variables/variables2.rs @@ -0,0 +1,47 @@ +// variables2.rs +// Make me compile! Scroll down for hints :) + +fn main() { + let x; + if x == 10 { + println!("Ten!"); + } else { + 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 5 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/old_curriculum/variables/variables3.rs b/old_curriculum/variables/variables3.rs new file mode 100644 index 0000000..165a277 --- /dev/null +++ b/old_curriculum/variables/variables3.rs @@ -0,0 +1,43 @@ +// variables3.rs +// Make me compile! Scroll down for hints :) + +fn main() { + let x = 3; + println!("Number {}", x); + 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/old_curriculum/variables/variables4.rs b/old_curriculum/variables/variables4.rs new file mode 100644 index 0000000..71ebf0f --- /dev/null +++ b/old_curriculum/variables/variables4.rs @@ -0,0 +1,45 @@ +// variables4.rs +// 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 5, and we're trying to use it on line 6, 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! |
