Type inference
๐ Scenario: You are writing a small Rust program to calculate the total price of items in a shopping cart. Rust can often figure out the type of variables automatically, which is called type inference. This helps you write cleaner code without always specifying types.
๐ฏ Goal: Build a Rust program that uses type inference to calculate the total price of items in a cart and print the result.
๐ What You'll Learn
Create a variable called
prices that holds a list of item prices as floating-point numbers.Create a variable called
total initialized to zero using type inference.Use a
for loop with variables price to iterate over prices and add each price to total.Print the total price using
println!.๐ก Why This Matters
๐ Real World
Type inference helps programmers write cleaner and shorter code without losing safety. It is used in many Rust programs to reduce clutter.
๐ผ Career
Understanding type inference is important for Rust developers to write efficient and readable code, which is valuable in software development jobs.
Progress0 / 4 steps