In this video, explore a sample program that calculates the area of a rectangle to demonstrate the application of structs.
- [Instructor] To understand when we might want…to use structs, let's write a program…that calculates the area of a rectangle.…We'll start with single variables…and then refactor our program…until we're using structs instead.…Let's make a new project with Cargo and call it rectangles.…Let's CD into the folder we created.…
On Visual Studio Code, as usual, our main.rs has cleared it…for us with a println statement that we can demo.…So let's say the width of the rectangle is 30…and let's say the height of the rectangle is 50…and let's call a function to actually print this out.…So println macro which says, "Area is,"…and then a placeholder which calls the area function.…
And to the area function we pass…the width and height as parameters.…Now let's come down here and define the area function…with the function keyword, function area,…and then we say width is of unsigned 32 type,…and height is again of unsigned 32.…And it returns and unsigned 32.…So as you may remember, the area of the rectangle…is width times height; so this is neat.…
- Installing Rust
- Data types
- Adjusting memory allocation
- Setting up race conditions
- Working with functions
- Exploring references and borrowing
- Using structs
- Method syntax
- Demonstrating recoverable errors with Result
- Working with enums
- Getting user input
Skill Level Beginner
1. Hello, Rust!
2. Basic Programming Concepts
3. Ownership in Rust
4. Data Handling and Pattern Matching in Rust
5. Error Handling
6. Sample Rust Project: The Guessing Game
Next steps1m 14s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.