I still don't really understand the use of memory and storage in solidity. I have written a simple code to store information from books indexed by a key, as in the example below. Since I'm using a mapping, the compiler throws an error if I try to declare books as storage. But he forces me to declare the new book as a memory. Apparently the code works, but I don't understand what is happening behind the scenes. Is the books array a storage-type variable? and what is it referencing to, exactly? Grateful if anyone can explain.
pragma solidity >=0.7.0 <0.9.0;
contract RegisterBook {
struct Book {
string info1;
string info2;
}
mapping(string => Book) public books;
function registerBook(string memory info1, string memory info2, string memory key) public returns (bool success) {
Book memory newBook;
newBook.info1 = info1;
newBook.info2 = info2;
books[key] = newBook;
return true;
}
}