Skip to content

Commit 59d7fdb

Browse files
JulienBerkaneDana Binkley
authored andcommitted
Clean commit
1 parent 9212769 commit 59d7fdb

5 files changed

Lines changed: 143 additions & 47 deletions

File tree

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,17 @@
1-
=================================
2-
Exercise: Calculate Total Price
3-
=================================
1+
=====
2+
Lab
3+
=====
44

5-
-------------
6-
The Problem
7-
-------------
5+
------------------
6+
Lab Instructions
7+
------------------
88

9-
**Goal:** Calculate the total price for an order
9+
- Solve for compilation errors
1010

11-
- The code below will not compile!
11+
- Follow the hints!
1212

13-
- Read the compiler error and use what you've learned about
14-
**Numeric Strictness** and **Casting** to fix it
13+
- Success is
1514

16-
.. code:: rust
15+
- Code that compiles
1716

18-
fn main() {
19-
let item_count: i32 = 15;
20-
let price_per_item: f64 = 4.99;
21-
22-
// Problem code!
23-
let total_price = item_count * price_per_item;
24-
25-
println!("Total items: {item_count}");
26-
println!("Total price: ${total_price}");
27-
}
28-
29-
--------------
30-
The Solution
31-
--------------
32-
33-
- The error was :error:`cannot multiply 'i32' by 'f64'`
34-
35-
- To fix it, you must **expicitly cast** the :rust:`i32` to an
36-
:rust:`f64` using the :rust:`as` keyword
37-
38-
.. code:: rust
39-
40-
fn main() {
41-
let item_count: i32 = 15;
42-
let price_per_item: f64 = 4.99;
43-
44-
// We cast the integer to a float to match the price
45-
let total_price = (item_count as f64) * price_per_item;
46-
47-
println!("Total items: {item_count}");
48-
println!("Total price: ${total_price}");
49-
}
17+
- ...and that follows any behavior indicated within the hints!
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
[package]
2-
name = "lab"
2+
name = "answer"
33
version = "1.0.0"
44
edition = "2024"
55

66
[dependencies]
77
# Leave this empty for now
8+
9+
Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
1+
//! Lab (answer)
2+
//! Types and Values
3+
//!
4+
//! Fix all the compile errors below and/or follow the hints provided
5+
//!
6+
7+
#![allow(unused_assignments)]
8+
#![allow(unused_variables)]
9+
110
fn main() {
2-
println!("TBD");
11+
// TASK 1 - Variable Declaration
12+
// Hint: Variable declaration has a particular syntax using ":" for types and "=" for value and ending with a ";"
13+
let apples = 5;
14+
let person = "Alice";
15+
16+
// TASK 2 - Mutable Variable
17+
// Hint: Keyword "mut" makes a variable mutable. Add a declaration to make 'money' mutable
18+
let mut money = 10;
19+
money = money + 20;
20+
21+
// TASK 3 - Variable Types
22+
// Hint: Initial value must match the types
23+
let initial : char = 'a';
24+
let big_number : u32 = 150000;
25+
26+
// TASK 4 - Type Inference and Static Type
27+
// Hint: Type is inferred by initial value. Numeric literals must match the type assigned
28+
let mut my_float = 6.5;
29+
my_float = 5.0;
30+
let mut my_int = 18;
31+
my_int += 7;
32+
33+
// TASK 5 - Base Numeric Literal
34+
// Hint: Convert the value into different bases and verify the display
35+
let binary = 0b1111_0000;
36+
let decimal = 240;
37+
let octal = 0o360;
38+
let hexadecimal = 0xf0;
39+
40+
println!("TASK5 => binary : {}", binary);
41+
println!("TASK5 => decimal : {}", decimal);
42+
println!("TASK5 => octal : {}", octal);
43+
println!("TASK5 => hexadecimal: {}", hexadecimal);
44+
45+
// TASK 6 - Numeric Conversion
46+
// Hint: Keyword "as" tells the compiler to interpret a value as a different type
47+
let my_int_2: i32 = 10;
48+
let my_float_2: f64 = 5.5;
49+
let sum = my_int_2 as f64 + my_float_2;
50+
51+
// TASK 7 - Arithmetic and the Exponent Trap
52+
// Hint: Code compiles but the value is not correct
53+
let exponent = 5_i32.pow(2);
54+
println!("TASK7 => 5 to the power of 2 equals: {}", exponent);
55+
56+
// TASK 8 - Arithmetic Division
57+
// Code compiles but the value is not correct
58+
// How do we print the decimal value here?
59+
// Hint: Remember division behavior depends on the type
60+
let precise = 7.0 / 3.0;
61+
println!("TASK8 => 7 divided by 3 equals: {}", precise);
62+
// Using float literals for this division is too much
63+
// How do we get an integer result here?
64+
let too_precise = 8 / 2;
65+
println!("TASK8 => 8 divided by 2 equals: {:?}", too_precise);
366
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[package]
2-
name = "lab"
2+
name = "prompt"
33
version = "1.0.0"
44
edition = "2024"
55

66
[dependencies]
77
# Leave this empty for now
8+
Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
1+
//! Lab (prompt)
2+
//! Types and Values
3+
//!
4+
//! Fix all the compile errors below and/or follow the hints provided
5+
//!
6+
7+
#![allow(unused_assignments)]
8+
#![allow(unused_variables)]
9+
110
fn main() {
2-
println!("TBD");
11+
// TASK 1 - Variable Declaration
12+
// Hint: Variable declaration has a particular syntax using ":" for types and "=" for value and ending with a ";"
13+
let apples := 5;
14+
let person = "Alice"
15+
16+
// TASK 2 - Mutable Variable
17+
// Hint: Keyword "mut" makes a variable mutable. Add a declaration to make 'money' mutable
18+
money = money + 20;
19+
20+
// TASK 3 - Variable Types
21+
// Hint: Initial value must match the types
22+
let initial : i32 = 'a';
23+
let big_number : u8 = 150000;
24+
25+
// TASK 4 - Type Inference and Static Type
26+
// Hint: Type is inferred by initial value. Numeric literals must match type assigned
27+
let mut my_float = 6.5;
28+
my_float = 5;
29+
let mut my_int = 18;
30+
my_int += 7.5;
31+
32+
// TASK 5 - Base Numeric Literal
33+
// Hint: Convert the value into different bases and verify the display
34+
let binary = 0b1111_0000;
35+
let decimal = ;
36+
let octal = ;
37+
let hexadecimal = ;
38+
39+
println!("TASK5 => binary : {}", binary);
40+
println!("TASK5 => decimal : {}", decimal);
41+
println!("TASK5 => octal : {}", octal);
42+
println!("TASK5 => hexadecimal: {}", hexadecimal);
43+
44+
// TASK 6 - Numeric Conversion
45+
// Hint: Keyword "as" tells the compiler to interpret a value as a different type
46+
let my_int_2: i32 = 10;
47+
let my_float_2: f64 = 5.5;
48+
let sum = my_int_2 + my_float_2;
49+
50+
// TASK 7 - Arithmetic and the Exponent Trap
51+
// Hint: Code compiles but the value is not correct
52+
let exponent = 5_i32 ^ 2;
53+
println!("TASK7 => 5 to the power of 2 equals: {}", exponent);
54+
55+
// TASK 8 - Arithmetic Division
56+
// Code compiles but the value is not correct.
57+
// How do we print the decimal value here?
58+
// Hint: Remember division behavior depends on the type
59+
let precise = 7 / 3;
60+
println!("TASK8 => 7 divided by 3 equals: {}", precise);
61+
// Using float literals for this division is too much
62+
// How do we get an integer result here?
63+
let too_precise = 8.000 / 2.000;
64+
println!("TASK8 => 8 divided by 2 equals: {:?}", too_precise);
365
}

0 commit comments

Comments
 (0)