From the course: Rust Essential Training
Unlock the full course today
Join today to access over 22,500 courses taught by industry experts or purchase this course individually.
Solution: Find the average - Rust Tutorial
From the course: Rust Essential Training
Solution: Find the average
(upbeat music) - To create my solution for the arithmetic average challenge, I'll begin by declaring a new variable, named average, and then calculating the value to assign average by writing out the basic equation to average together three variables A plus B plus C divided by 3. Notice that I've written 3 with a decimal place, so it will be stored as a floating-point value. I did that because I want the result of the division operation to be stored as a float in case the average has a fractional value. Next, I'll put parentheses around A, B, and C. That will enforce order of operations to make sure they get added together to form the numerator before dividing by 3. Now, let's try running the program. And we get an error. It failed to compile because we can't add together A and B which have different data types. A is an integer, and B is a float. So let's cast A to be handled as a 64 bit float like B. We'll…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Declaring variables5m 46s
-
Integer data types6m 51s
-
Floating-point data types5m 34s
-
Arithmetic operations6m 37s
-
Formatting print statements5m 40s
-
Bitwise operations10m 46s
-
Boolean data type and operations5m 4s
-
Comparison operations3m 5s
-
Char data types2m 36s
-
Challenge: Find the average1m 20s
-
Solution: Find the average3m 20s
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-