-
Notifications
You must be signed in to change notification settings - Fork 6
/
associated-constants.html
309 lines (267 loc) · 10.5 KB
/
associated-constants.html
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="rustdoc">
<title>Le costanti associate</title>
<link rel="stylesheet" type="text/css" href="rustbook.css">
</head>
<body class="rustdoc">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<div id="nav">
<button id="toggle-nav">
<span class="sr-only">Toggle navigation</span>
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</button>
</div>
<div id='toc' class='mobile-hidden'>
<ul class='chapter'>
<li><a href='README.html'><b>1.</b> Introduzione</a>
</li>
<li><a href='getting-started.html'><b>2.</b> Come Iniziare</a>
</li>
<li><a href='guessing-game.html'><b>3.</b> Tutorial: Gioco-indovina</a>
</li>
<li><a href='syntax-and-semantics.html'><b>4.</b> Sintassi e semantica</a>
<ul class='section'>
<li><a href='variable-bindings.html'><b>4.1.</b> Legami di variabili</a>
</li>
<li><a href='functions.html'><b>4.2.</b> Funzioni</a>
</li>
<li><a href='primitive-types.html'><b>4.3.</b> Tipi primitivi</a>
</li>
<li><a href='comments.html'><b>4.4.</b> Commenti</a>
</li>
<li><a href='if.html'><b>4.5.</b> if</a>
</li>
<li><a href='loops.html'><b>4.6.</b> Cicli</a>
</li>
<li><a href='vectors.html'><b>4.7.</b> Vettori</a>
</li>
<li><a href='ownership.html'><b>4.8.</b> Possesso</a>
</li>
<li><a href='references-and-borrowing.html'><b>4.9.</b> Riferimenti e prestito</a>
</li>
<li><a href='lifetimes.html'><b>4.10.</b> Tempo di vita</a>
</li>
<li><a href='mutability.html'><b>4.11.</b> Mutabilità</a>
</li>
<li><a href='structs.html'><b>4.12.</b> Strutture</a>
</li>
<li><a href='enums.html'><b>4.13.</b> Enumerazioni</a>
</li>
<li><a href='match.html'><b>4.14.</b> Match</a>
</li>
<li><a href='patterns.html'><b>4.15.</b> Pattern</a>
</li>
<li><a href='method-syntax.html'><b>4.16.</b> Sintassi dei metodi</a>
</li>
<li><a href='strings.html'><b>4.17.</b> Stringhe</a>
</li>
<li><a href='generics.html'><b>4.18.</b> Genericità</a>
</li>
<li><a href='traits.html'><b>4.19.</b> Tratti</a>
</li>
<li><a href='drop.html'><b>4.20.</b> Drop</a>
</li>
<li><a href='if-let.html'><b>4.21.</b> `if let`</a>
</li>
<li><a href='trait-objects.html'><b>4.22.</b> Oggetti-tratti</a>
</li>
<li><a href='closures.html'><b>4.23.</b> Chiusure</a>
</li>
<li><a href='ufcs.html'><b>4.24.</b> Sintassi universale di chiamata di funzione</a>
</li>
<li><a href='crates-and-modules.html'><b>4.25.</b> Crate e moduli</a>
</li>
<li><a href='const-and-static.html'><b>4.26.</b> `const` e `static`</a>
</li>
<li><a href='attributes.html'><b>4.27.</b> Attributi</a>
</li>
<li><a href='type-aliases.html'><b>4.28.</b> Alias tramite `type`</a>
</li>
<li><a href='casting-between-types.html'><b>4.29.</b> Forzatura di tipo</a>
</li>
<li><a href='associated-types.html'><b>4.30.</b> Tipi associati</a>
</li>
<li><a href='unsized-types.html'><b>4.31.</b> Tipi non dimensionati</a>
</li>
<li><a href='operators-and-overloading.html'><b>4.32.</b> Operatori e sovraccaricamento</a>
</li>
<li><a href='deref-coercions.html'><b>4.33.</b> Coercizione Deref</a>
</li>
<li><a href='macros.html'><b>4.34.</b> Le macro</a>
</li>
<li><a href='raw-pointers.html'><b>4.35.</b> Puntatori grezzi</a>
</li>
<li><a href='unsafe.html'><b>4.36.</b> `unsafe`</a>
</li>
</ul>
</li>
<li><a href='effective-rust.html'><b>5.</b> Rust efficace</a>
<ul class='section'>
<li><a href='the-stack-and-the-heap.html'><b>5.1.</b> Lo stack e lo heap</a>
</li>
<li><a href='testing.html'><b>5.2.</b> Collaudo</a>
</li>
<li><a href='conditional-compilation.html'><b>5.3.</b> Compilazione condizionale</a>
</li>
<li><a href='documentation.html'><b>5.4.</b> Documentazione</a>
</li>
<li><a href='iterators.html'><b>5.5.</b> Iteratori</a>
</li>
<li><a href='concurrency.html'><b>5.6.</b> Concorrenza</a>
</li>
<li><a href='error-handling.html'><b>5.7.</b> Gestione degli errori</a>
</li>
<li><a href='choosing-your-guarantees.html'><b>5.8.</b> Scegliere le garanzie</a>
</li>
<li><a href='ffi.html'><b>5.9.</b> FFI</a>
</li>
<li><a href='borrow-and-asref.html'><b>5.10.</b> Prestito e AsRef</a>
</li>
<li><a href='release-channels.html'><b>5.11.</b> Canali di rilascio</a>
</li>
<li><a href='using-rust-without-the-standard-library.html'><b>5.12.</b> Usare Rust senza la libreria standard</a>
</li>
</ul>
</li>
<li><a href='nightly-rust.html'><b>6.</b> Rust notturno</a>
<ul class='section'>
<li><a href='compiler-plugins.html'><b>6.1.</b> Plugin del compilatore</a>
</li>
<li><a href='inline-assembly.html'><b>6.2.</b> Assembly in-line</a>
</li>
<li><a href='no-stdlib.html'><b>6.3.</b> Omettere la libreria stdandard</a>
</li>
<li><a href='intrinsics.html'><b>6.4.</b> Intrinseci</a>
</li>
<li><a href='lang-items.html'><b>6.5.</b> Elementi "lang"</a>
</li>
<li><a href='advanced-linking.html'><b>6.6.</b> Link avanzato</a>
</li>
<li><a href='benchmark-tests.html'><b>6.7.</b> Collaudi prestazionali</a>
</li>
<li><a href='box-syntax-and-patterns.html'><b>6.8.</b> Sintassi di box e relativi pattern</a>
</li>
<li><a href='slice-patterns.html'><b>6.9.</b> Pattern di slice</a>
</li>
<li><a class='active' href='associated-constants.html'><b>6.10.</b> Costanti associate</a>
</li>
<li><a href='custom-allocators.html'><b>6.11.</b> Allocatori personalizzati</a>
</li>
</ul>
</li>
<li><a href='glossary.html'><b>7.</b> Glossario</a>
</li>
<li><a href='syntax-index.html'><b>8.</b> Indice analitico della sintassi</a>
</li>
</ul>
</div>
<div id='page-wrapper'>
<div id='page'>
<h1 class="title">Le costanti associate</h1>
<p>Con la caratteristica delle <code>associated_consts</code>, si possono definire
costanti così:</p>
<span class='rusttest'>#![feature(associated_consts)]
trait Foo {
const ID: i32;
}
impl Foo for i32 {
const ID: i32 = 1;
}
fn main() {
assert_eq!(1, i32::ID);
}
</span><pre class='rust rust-example-rendered'>
<span class='attribute'>#<span class='op'>!</span>[<span class='ident'>feature</span>(<span class='ident'>associated_consts</span>)]</span>
<span class='kw'>trait</span> <span class='ident'>Foo</span> {
<span class='kw'>const</span> <span class='ident'>ID</span>: <span class='ident'>i32</span>;
}
<span class='kw'>impl</span> <span class='ident'>Foo</span> <span class='kw'>for</span> <span class='ident'>i32</span> {
<span class='kw'>const</span> <span class='ident'>ID</span>: <span class='ident'>i32</span> <span class='op'>=</span> <span class='number'>1</span>;
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='number'>1</span>, <span class='ident'>i32</span>::<span class='ident'>ID</span>);
}</pre>
<p>Qualunque implementatore di <code>Foo</code> dovrà definire <code>ID</code>. Senza la definizione:</p>
<span class='rusttest'>#![feature(associated_consts)]
fn main() {
trait Foo {
const ID: i32;
}
impl Foo for i32 {
}
}</span><pre class='rust rust-example-rendered'>
<span class='attribute'>#<span class='op'>!</span>[<span class='ident'>feature</span>(<span class='ident'>associated_consts</span>)]</span>
<span class='kw'>trait</span> <span class='ident'>Foo</span> {
<span class='kw'>const</span> <span class='ident'>ID</span>: <span class='ident'>i32</span>;
}
<span class='kw'>impl</span> <span class='ident'>Foo</span> <span class='kw'>for</span> <span class='ident'>i32</span> {
}</pre>
<p>si ottiene:</p>
<pre><code class="language-text">error: not all trait items implemented, missing: `ID` [E0046]
impl Foo for i32 {
}
</code></pre>
<p>Si può implementare anche un valore di default:</p>
<span class='rusttest'>#![feature(associated_consts)]
trait Foo {
const ID: i32 = 1;
}
impl Foo for i32 {
}
impl Foo for i64 {
const ID: i32 = 5;
}
fn main() {
assert_eq!(1, i32::ID);
assert_eq!(5, i64::ID);
}
</span><pre class='rust rust-example-rendered'>
<span class='attribute'>#<span class='op'>!</span>[<span class='ident'>feature</span>(<span class='ident'>associated_consts</span>)]</span>
<span class='kw'>trait</span> <span class='ident'>Foo</span> {
<span class='kw'>const</span> <span class='ident'>ID</span>: <span class='ident'>i32</span> <span class='op'>=</span> <span class='number'>1</span>;
}
<span class='kw'>impl</span> <span class='ident'>Foo</span> <span class='kw'>for</span> <span class='ident'>i32</span> {
}
<span class='kw'>impl</span> <span class='ident'>Foo</span> <span class='kw'>for</span> <span class='ident'>i64</span> {
<span class='kw'>const</span> <span class='ident'>ID</span>: <span class='ident'>i32</span> <span class='op'>=</span> <span class='number'>5</span>;
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='number'>1</span>, <span class='ident'>i32</span>::<span class='ident'>ID</span>);
<span class='macro'>assert_eq</span><span class='macro'>!</span>(<span class='number'>5</span>, <span class='ident'>i64</span>::<span class='ident'>ID</span>);
}</pre>
<p>Come si vede, quando si implementa <code>Foo</code>, si può lasciare la costante
non implementata, come per <code>i32</code>. In tal caso si userà il valore di default.
Ma, come per <code>i64</code>, si può anche aggiungere una ridefinizione.</p>
<p>Le costanti associate non devono necessariamente essere associate a un tratto.
Un blocco <code>impl</code> funziona bene anche per una <code>struct</code> o una <code>enum</code>:</p>
<span class='rusttest'>#![feature(associated_consts)]
fn main() {
struct Foo;
impl Foo {
const FOO: u32 = 3;
}
}</span><pre class='rust rust-example-rendered'>
<span class='attribute'>#<span class='op'>!</span>[<span class='ident'>feature</span>(<span class='ident'>associated_consts</span>)]</span>
<span class='kw'>struct</span> <span class='ident'>Foo</span>;
<span class='kw'>impl</span> <span class='ident'>Foo</span> {
<span class='kw'>const</span> <span class='ident'>FOO</span>: <span class='ident'>u32</span> <span class='op'>=</span> <span class='number'>3</span>;
}</pre>
<script type="text/javascript">
window.playgroundUrl = "https://play.rust-lang.org";
</script>
<script src='rustbook.js'></script>
<script src='playpen.js'></script>
</div></div>
</body>
</html>