What does "re:" and "im:" mean in Rust?

4

I want to know what re and im mean / do

let mut z = Complex { re: 0.0, im: 0,0 };

I'm learning Rust from the Programming Rust book, and this re: and im: should have already appeared before, but just now I've bothered to search.

What I found before arriving here was that im: is a way to create immutable code structures (immutable data structures ), but nothing about re , which kick that can be reusable.

Another thing I found from im: was also in the Rust documentation where it said it was a crate , so I'm not sure if it's something else entirely (this is the only dependency listed in% with% ")

    
asked by anonymous 03.12.2018 / 13:53

2 answers

7

Rust allows you to initialize an object with a literal form. This form is composed of the type name, keys that will indicate the beginning and end of this literal, as if it were the quotation marks of a string , and then you will place the members you want to initialize the object data.

Rust has no constructor like other languages, it can have simple functions that serve as constructors, but the most used form is the pure and simple initialization of this form of literal.

So it looks like a function call with named arguments, but what you're doing is using the names of the members of struct and their values. Then what is before : is the name of the member and what is after : is the value that will be assigned to this member.

In this example which is a type Complex , re is the real part of the number and im is the imaginary part. It has nothing to do with what you speculated on the question, it is much simpler than that. This information has to do with the type of data there, Complex and not with language rules. The language only determines the syntax of this object initializer.

Another hypothetical example:

let joao = Funcionario { nome : "João", salario : 1000 };

Got it? They are only members of struct , and bad words are called chosen, actually looks like C and not Rust, which usually cuts a lot.

    
03.12.2018 / 14:18
4

This has no relation to immutable or reusable.

Probably at some point in the book a structure similar to:

struct Complex {
    re: f64,
    im: f64
}

That represents a complex number . re represents the real part, while im represents the imaginary part.

When doing:

let mut z = Complex { re: 0.0, im: 0.0 };

You are basically creating an object z , which follows the Complex structure, having a value of zero in both the real part and the imaginary part.

You can read more about struct at Using Structs to Structure Related Data .

    
03.12.2018 / 14:13