mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 18:54:32 -06:00
Add units to the output
This commit is contained in:
+13
-4
@@ -79,7 +79,10 @@ fn main() {
|
||||
let length1 = 50;
|
||||
let width1 = 30;
|
||||
|
||||
println!("The area of the rectangle is {}", area(length1, width1));
|
||||
println!(
|
||||
"The area of the rectangle is {} square pixels.",
|
||||
area(length1, width1)
|
||||
);
|
||||
}
|
||||
|
||||
fn area(length: u32, width: u32) -> u32 {
|
||||
@@ -90,7 +93,7 @@ fn area(length: u32, width: u32) -> u32 {
|
||||
Let's try running this program with `cargo run`:
|
||||
|
||||
```bash
|
||||
The area of the rectangle is 1500
|
||||
The area of the rectangle is 1500 square pixels.
|
||||
```
|
||||
|
||||
### Refactoring with Tuples
|
||||
@@ -120,7 +123,10 @@ Filename: src/main.rs
|
||||
fn main() {
|
||||
let rect1 = (50, 30);
|
||||
|
||||
println!("The area of the rectangle is {}", area(rect1));
|
||||
println!(
|
||||
"The area of the rectangle is {} square pixels.",
|
||||
area(rect1)
|
||||
);
|
||||
}
|
||||
|
||||
fn area(dimensions: (u32, u32)) -> u32 {
|
||||
@@ -161,7 +167,10 @@ struct Rectangle {
|
||||
fn main() {
|
||||
let rect1 = Rectangle { length: 50, width: 30 };
|
||||
|
||||
println!("The area of the rectangle is {}", area(&rect1));
|
||||
println!(
|
||||
"The area of the rectangle is {} square pixels.",
|
||||
area(&rect1)
|
||||
);
|
||||
}
|
||||
|
||||
fn area(rectangle: &Rectangle) -> u32 {
|
||||
|
||||
Reference in New Issue
Block a user