Hey @Tesh
Why can’t we just return user.balance in the last function below?
Let’s clarify the way mappings work.
mapping ( key => value ) mapping_name.
In order to access data in a mapping you always have to provide a key.
In order to access a mapping:
mapping_name[key];
In this case you have a mapping that points to a struct, let’s simplify:
pragma solidity 0.7.5;
contract TestContract {
struct Test{
uint num;
}
mapping(uint => Test) public tests;
}
Let’s now translate mapping(uint => Test) public tests;
, you can read it as follow:
There is a mapping that asks for a key
of type uint and returns a struct called Test
.
Based on the sentence above, we can create a setter function:
function set(uint _id) public {
tests[_id].num = 10;
}
Our setter function asks for an _id
of type uint (that will be our mapping key).
We then set num
for key
: _id
to 10.
Now the getter function, remember that we always need a key
to access our mapping!
function get(uint _id) public view returns (uint) {
return tests[_id].num;
}
Our getter function asks for an _id
of type uint (that will be our mapping key).
We then return the number that is stored in tests[key].num.
I have used the example above as I think it will be more clear for you in order to understand mappings.
You can access mappings also by using Filip example (this will be mandatory in other cases).
Keep considering our contract above, let’s change the setter function:
function set(uint _id) public {
Test storage testsPointer = tests[_id];
testsPointer.num = 10;
}
This does exactly the same thing as the previous setter, is just wrote differently.
You can read it as:
Test storage testsPointer = tests[_id];
-> There is a struct called Test
that can be access with a mapping that uses tests[_id]
.
To make it short and write as less code as possibile, I will save tests[_id]
in something called testsPointer
.
So you can read testsPointer == tests[_id]
Now it’s easy for you to understand because is exactly the same thing as in our previous setter:
testsPointer.num = 10;
Which is the same as tests[_id].num = 10

Hope it’s clear!
Cheers,
Dani