blob: e7d9d0a51be83658a3f304ed6a9227aada11566e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>rust basic -- types and ownership | garhve's gibberish</title>
<link rel="shortcut icon" type="image/png" href="/images/favicon.png">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link id="stylesheet" rel="stylesheet" type="text/css" href="/dark.css">
<script type="text/javascript" src="/js/theme.js"></script>
</head>
<div class="header">
<div class="site_title">
<p><a href="/"><img src="https://blog.garhve.com/images/logo.png" alt="garhve's gibberish"
width="70" height=auto></a></p>
<p><a href="/"> garhve's gibberish</a></p>
</div>
<div class="menu">
<!-- <a href="/tags">tags</a>
--><a href="/categories">categories</a>
</div>
</div>
<body onload="getTheme()">
<section class="section">
<div class="container">
<p>
<div class="title_postpage">rust basic -- types and ownership</div>
</p>
<p>
<div class="date_postpage">2023-01-12</div>
<div class="taxonomies_postpage">
<a href="https://blog.garhve.com/categories/code/">/code</a>
 <a href="https://blog.garhve.com/tags/rust/">#rust</a>
</div>
</p>
<p>
<h2 id="types">Types</h2>
<blockquote>
<p>This is a summary of rust basic knowledge I learned from (Rust the Book)[https://doc.rust-lang.org/book/] chapter 1 ~ chapter 13
I will use C to compare with since I familiar it.</p>
</blockquote>
<p>There are a bunch of built-in types we can work with: <em>scalar types</em> and <em>compound types</em>.
Those scalar types are just alike as C language.</p>
<table><thead><tr><th>scalar types</th><th>rust</th><th>c</th></tr></thead><tbody>
<tr><td>integer</td><td>i(u)8/i16/i32(default)/i64</td><td>(unsigned) char/short/int/long</td></tr>
<tr><td>float</td><td>f32/f64(default)</td><td>float/double</td></tr>
<tr><td>boolean</td><td>true/false</td><td>True/False (non-zero/zero)</td></tr>
<tr><td>character</td><td>char</td><td>char</td></tr>
</tbody></table>
<p>The compound types are a bit different. rust provides many hands-on types where C type is not the same or needs to define explicitly.</p>
<table><thead><tr><th>compound type</th><th>rust</th><th>c</th></tr></thead><tbody>
<tr><td>struct</td><td>same</td><td>same</td></tr>
<tr><td>enum</td><td>enum element can be seen as key that could take value</td><td>represent numberic value, default starting from 0</td></tr>
<tr><td>array</td><td>vector</td><td>array</td></tr>
<tr><td>string</td><td>a wraper of vector</td><td>pointer of char *</td></tr>
<tr><td>tuple</td><td>exist</td><td>no such type in C</td></tr>
</tbody></table>
<p>Variable declaration in rust is kinda tricky.</p>
<p>A normal declare syntax would be like <code>let unmutable_variable = 3</code>. This will imply that variable is <code>i32</code> type
since the value we assign to is a integer and the default type for integer on most OS is <code>i32</code>.
We can explicit type by using type annotation: <code>let unmutable_variable: u8 = 3</code>. This will make variable
<code>unmutable_variable</code> is <code>u8</code> type and has 3 as its value. We need to assign a value
to variable if we don't annotate type while declaration, otherwise compiler would prompt error.</p>
<p>Notice that, declaration as above one won't allow us to change value, if we want to be able to change variable's
value at runtime, we need to explicit it, for security reason: </p>
<pre data-lang="rust" style="background-color:#2e3440;color:#d8dee9;" class="language-rust "><code class="language-rust" data-lang="rust"><span style="color:#81a1c1;">let mut</span><span> mutable_variable </span><span style="color:#81a1c1;">= </span><span style="color:#b48ead;">3</span><span style="color:#eceff4;">;
</span><span>println!(</span><span style="color:#a3be8c;">"first define: </span><span style="color:#ebcb8b;">{mutable_variable}</span><span style="color:#a3be8c;">"</span><span>)</span><span style="color:#eceff4;">;
</span><span>mutable_variable </span><span style="color:#81a1c1;">= </span><span style="color:#b48ead;">5</span><span style="color:#eceff4;">;
</span><span>
</span><span>println!(</span><span style="color:#a3be8c;">"change value to: </span><span style="color:#ebcb8b;">{mutable_variable}</span><span style="color:#a3be8c;">"</span><span>)</span><span style="color:#eceff4;">;
</span></code></pre>
<blockquote>
<p><code>println!()</code> is called macro, which would not cover for now.</p>
</blockquote>
</p>
</div>
</section>
</body>
<div class="footer">
 © garhve
</div>
</html>
|