Join Abhiram Ravikumar for an in-depth discussion in this video Return a value from a function, part of First Look: Rust.
- [Narrator] Functions can return values…to the core that calls them.…We don't name return values,…but we do declare their type after an arrow.…In Rust the written value of the function is synonymous…with the value of the final expression in the block…of the body of a function.…Let's create a function with two parameters,…both of which are i32 types.…Oops, looks like I missed an ln over here on line 13,…let me just save that,…and let's declare our new function.…
So use the fn keyword, and the name of the function here…is sum, and it takes in two parameters of type i32…which I am naming them x and y here.…And the return type would be i32 as well.…For now let's go ahead and calculate the sum.…So I just add x and y.…From the main function let's invoke this function.…(keyboard keys clacking)…Now since the return type of this function is an i32…we will assign it to a variable.…
So let a = sum.…So here the sum of 10 and five would be calculated…and stored in the variable a.…Let's go ahead and print that out.…(keyboard keys clacking)…
- 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
Programming Foundations: Data Structures (2014)with Simon Allardice2h 29m Intermediate
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.