--- author: Adam T. Carpenter, Carpenter Tutoring --- # Computational problem solving with _Rust_ ## Starting to program ``` ~~~fsays Starting to practice! ~~~ ``` [practice of computing using python](https://www.pearson.com/en-us/subject-catalog/p/practice-of-computing-using-python-the/p200000003329/9780137524839) [rust book](https://doc.rust-lang.org/book/) --- # Getting started We need to dive into language issues in order to work through problem-solving issues. Recall: ## 1. Think before you program ## 2. A program is a human-readable essay on problem solving (that also runs on a computer) --- # Practice! Learning to experiment is key to learning to program. If you have a question, try it out! Time for a new rule: ## 3. The best way to improve your programming an problem skills is to practice! --- # Calculate the circumference and area of a circle given its radius Formulas: ``` circumference = 2 * π * radius area = π * radius ^ 2 ``` Requirements: 1. We need to prompt the user for a radius TODO: --- # Calculate the circumference and area of a circle given its radius ```bash tmux display-popup 'rustc ./circumference.rs && ./circumference' ``` ```rust use std::f64::consts::PI; use std::io::stdin; // Calculate the area and circumference of a circle from its radius. // 1. Prompt for a radius input. // 2. Parse the input into a numeric radius. // 3. Apply the area and circumference formulas. // 4. Print out the results. fn main() { let mut input = String::new(); println!("Enter the radius of your circle: "); stdin().read_line(&mut input).expect("No radius given."); let radius: f64 = input.trim().parse().expect("Invalid radius given."); let circumference = 2.0 * PI * radius; let area = PI * radius.powi(2); println!("The circumference is: {circumference}, and the area is: {area}."); } ```