mirror of
https://github.com/Dhghomon/easy_rust
synced 2024-11-17 15:29:51 +00:00
deploy: f1cbf2edf7
This commit is contained in:
parent
6e7ad77cc7
commit
5d619e4a23
@ -168,7 +168,7 @@
|
||||
<p>The stack and the heap are two places to keep memory in computers. The important differences are:</p>
|
||||
<ul>
|
||||
<li>The stack is very fast, but the heap is not so fast. It's not super slow either, but the stack is always faster. But you can't just use the stack all the time, because:</li>
|
||||
<li>The stack needs to know the size of a variable at compile time. So simple variables like <code>i32</code> go on the stack, because we know their exact size. You always know that an <code>i32</code> is going to be 4 bytes, because 32 bits = 4 bytes. So <code>i32</code> can always go on the stack.</li>
|
||||
<li>Rust needs to know the size of a variable at compile time. So simple variables like <code>i32</code> go on the stack, because we know their exact size. You always know that an <code>i32</code> is going to be 4 bytes, because 32 bits = 4 bytes. So <code>i32</code> can always go on the stack.</li>
|
||||
<li>But some types don't know the size at compile time. But the stack needs to know the exact size. So what do you do? First you put the data in the heap, because the heap can have any size of data. And then to find it a pointer goes on the stack. This is fine because we always know the size of a pointer. So then the computer first goes to the stack, reads the pointer, and follows it to the heap where the data is.</li>
|
||||
</ul>
|
||||
<p>Pointers sound complicated, but they are easy. Pointers are like a table of contents in a book. Imagine this book:</p>
|
||||
|
@ -869,7 +869,7 @@ fn main() {
|
||||
<p>The stack and the heap are two places to keep memory in computers. The important differences are:</p>
|
||||
<ul>
|
||||
<li>The stack is very fast, but the heap is not so fast. It's not super slow either, but the stack is always faster. But you can't just use the stack all the time, because:</li>
|
||||
<li>The stack needs to know the size of a variable at compile time. So simple variables like <code>i32</code> go on the stack, because we know their exact size. You always know that an <code>i32</code> is going to be 4 bytes, because 32 bits = 4 bytes. So <code>i32</code> can always go on the stack.</li>
|
||||
<li>Rust needs to know the size of a variable at compile time. So simple variables like <code>i32</code> go on the stack, because we know their exact size. You always know that an <code>i32</code> is going to be 4 bytes, because 32 bits = 4 bytes. So <code>i32</code> can always go on the stack.</li>
|
||||
<li>But some types don't know the size at compile time. But the stack needs to know the exact size. So what do you do? First you put the data in the heap, because the heap can have any size of data. And then to find it a pointer goes on the stack. This is fine because we always know the size of a pointer. So then the computer first goes to the stack, reads the pointer, and follows it to the heap where the data is.</li>
|
||||
</ul>
|
||||
<p>Pointers sound complicated, but they are easy. Pointers are like a table of contents in a book. Imagine this book:</p>
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user