From the course: First Look: Rust

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Building structs using values

Building structs using values - Rust Tutorial

From the course: First Look: Rust

Start my 1-month free trial

Building structs using values

[Man] Consider a scenario where you have a username and an email address and you'd like to build a struct from those given values. Rust has a shorthand for precisely such an operation. Let's write a new function over here build user. So this function take email as a string input followed by the username as a string and then the return type of this function is a struct which is the user here so since you're building the struct here we start with the name of the struct we want to build followed be the curly braces and all the values that go into the struct we write email here and then username since we are passing email and username from an external function call we are taking that from the parameters data key whereas we have two other variables active that we are setting to true and the sign in count which is set to one. So we are setting these values explicitly because we are not passing them in the parameter list so let me save this. So this is how you will build a particular struct…

Contents