mirror of
https://github.com/Dhghomon/easy_rust
synced 2024-11-17 15:29:51 +00:00
deploy: f5b59c27f5
This commit is contained in:
parent
eebc40a86b
commit
6f91703301
@ -181,7 +181,7 @@ fn main() {
|
||||
}
|
||||
</code></pre></pre>
|
||||
<p>After <code>takes_a_string</code> takes <code>user_name</code>, you can't use it anymore. Here that is no problem: you can just give it <code>user_name.clone()</code>. But sometimes a variable is part of a struct, and maybe you can't clone the struct. Or maybe the <code>String</code> is really long and you don't want to clone it. These are some reasons for <code>Rc</code>, which lets you have more than one owner. An <code>Rc</code> is like a good office worker: <code>Rc</code> writes down who has ownership, and how many. Then once the number of owners goes down to 0, the variable can disappear.</p>
|
||||
<p>Here's how you use an <code>Rc</code>. First imagine two structs: one called <code>City</code>, and another called <code>Cities</code>. <code>City</code> has information for one city, and <code>Cities</code> puts all the cities together in <code>Vec</code>s.</p>
|
||||
<p>Here's how you use an <code>Rc</code>. First imagine two structs: one called <code>City</code>, and another called <code>CityData</code>. <code>City</code> has information for one city, and <code>CityData</code> puts all the cities together in <code>Vec</code>s.</p>
|
||||
<pre><pre class="playground"><code class="language-rust">#[derive(Debug)]
|
||||
struct City {
|
||||
name: String,
|
||||
@ -240,7 +240,7 @@ struct City {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Cities {
|
||||
struct CityData {
|
||||
names: Vec<String>,
|
||||
histories: Vec<Rc<String>>,
|
||||
}
|
||||
|
@ -6590,7 +6590,7 @@ fn main() {
|
||||
}
|
||||
</code></pre></pre>
|
||||
<p>After <code>takes_a_string</code> takes <code>user_name</code>, you can't use it anymore. Here that is no problem: you can just give it <code>user_name.clone()</code>. But sometimes a variable is part of a struct, and maybe you can't clone the struct. Or maybe the <code>String</code> is really long and you don't want to clone it. These are some reasons for <code>Rc</code>, which lets you have more than one owner. An <code>Rc</code> is like a good office worker: <code>Rc</code> writes down who has ownership, and how many. Then once the number of owners goes down to 0, the variable can disappear.</p>
|
||||
<p>Here's how you use an <code>Rc</code>. First imagine two structs: one called <code>City</code>, and another called <code>Cities</code>. <code>City</code> has information for one city, and <code>Cities</code> puts all the cities together in <code>Vec</code>s.</p>
|
||||
<p>Here's how you use an <code>Rc</code>. First imagine two structs: one called <code>City</code>, and another called <code>CityData</code>. <code>City</code> has information for one city, and <code>CityData</code> puts all the cities together in <code>Vec</code>s.</p>
|
||||
<pre><pre class="playground"><code class="language-rust">#[derive(Debug)]
|
||||
struct City {
|
||||
name: String,
|
||||
@ -6649,7 +6649,7 @@ struct City {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Cities {
|
||||
struct CityData {
|
||||
names: Vec<String>,
|
||||
histories: Vec<Rc<String>>,
|
||||
}
|
||||
|
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