1
- % Box Syntax and Patterns
1
+ % La sintassi e i pattern di Box
2
2
3
- Currently the only stable way to create a ` Box ` is via the ` Box::new ` method.
4
- Also it is not possible in stable Rust to destructure a ` Box ` in a match
5
- pattern. The unstable ` box ` keyword can be used to both create and destructure
6
- a ` Box ` . An example usage would be:
3
+ Attualmente, l'unico modo stabile di creare un ` Box ` è tramite il metodo
4
+ ` Box::new ` . Inoltre, nella versione stabile di Rust, non è possibile
5
+ destrutturare un ` Box ` in un pattern di ` match ` . La parola-chiave instabile
6
+ ` box ` si può usare sia per creare che per destrutturare un ` Box ` .
7
+ Un esempio di utilizzo sarebbe:
7
8
8
9
``` rust
9
10
#![feature(box_syntax, box_patterns)]
@@ -12,89 +13,92 @@ fn main() {
12
13
let b = Some (box 5 );
13
14
match b {
14
15
Some (box n ) if n < 0 => {
15
- println! (" Box contains negative number {}" , n );
16
+ println! (" Il box contiene il numero negativo {}" , n );
16
17
},
17
18
Some (box n ) if n >= 0 => {
18
- println! (" Box contains non-negative number {}" , n );
19
+ println! (" Il box contiene il numero non negativo {}" , n );
19
20
},
20
21
None => {
21
- println! (" No box" );
22
+ println! (" Nessun box" );
22
23
},
23
24
_ => unreachable! ()
24
25
}
25
26
}
26
27
```
27
28
28
- Note that these features are currently hidden behind the ` box_syntax ` (box
29
- creation) and ` box_patterns ` (destructuring and pattern matching) gates
30
- because the syntax may still change in the future.
29
+ Si noti che queste caratteristiche sono attualmente nascoste dietro i gate
30
+ ` box_syntax ` (per la creazione dei box) e ` box_patterns `
31
+ (per la destrutturazione e il pattern matching), perché la sintassi
32
+ può ancora cambiare in futuro.
31
33
32
- # Returning Pointers
34
+ # Restituire puntatori
33
35
34
- In many languages with pointers, you'd return a pointer from a function
35
- so as to avoid copying a large data structure. For example :
36
+ In molti linguaggi dotati di puntatori, si restituirebbe un puntatore
37
+ da una funzione per evitare di copiare una grande struttura dati. Per esempio :
36
38
37
39
``` rust
38
- struct BigStruct {
39
- one : i32 ,
40
- two : i32 ,
41
- // etc
42
- one_hundred : i32 ,
40
+ struct GrossaStruct {
41
+ uno : i32 ,
42
+ due : i32 ,
43
+ // ecc
44
+ cento : i32 ,
43
45
}
44
46
45
- fn foo (x : Box <BigStruct >) -> Box <BigStruct > {
47
+ fn foo (x : Box <GrossaStruct >) -> Box <GrossaStruct > {
46
48
Box :: new (* x )
47
49
}
48
50
49
51
fn main () {
50
- let x = Box :: new (BigStruct {
51
- one : 1 ,
52
- two : 2 ,
53
- one_hundred : 100 ,
52
+ let x = Box :: new (GrossaStruct {
53
+ uno : 1 ,
54
+ due : 2 ,
55
+ cento : 100 ,
54
56
});
55
57
56
58
let y = foo (x );
57
59
}
58
60
```
59
61
60
- The idea is that by passing around a box, you're only copying a pointer, rather
61
- than the hundred ` i32 ` s that make up the ` BigStruct ` .
62
+ L' idea è che passando un box, sia sta solo copiando un puntatore, invece
63
+ dei cento ` i32 ` che costituiscono la ` GrossaStruct ` .
62
64
63
- This is an antipattern in Rust. Instead, write this :
65
+ Questo in Rust è un antipattern. Invece, si scrive così :
64
66
65
67
``` rust
66
68
#![feature(box_syntax)]
67
69
68
- struct BigStruct {
69
- one : i32 ,
70
- two : i32 ,
71
- // etc
72
- one_hundred : i32 ,
70
+ struct GrossaStruct {
71
+ uno : i32 ,
72
+ due : i32 ,
73
+ // ecc
74
+ cento : i32 ,
73
75
}
74
76
75
- fn foo (x : Box <BigStruct >) -> BigStruct {
77
+ fn foo (x : Box <GrossaStruct >) -> GrossaStruct {
76
78
* x
77
79
}
78
80
79
81
fn main () {
80
- let x = Box :: new (BigStruct {
81
- one : 1 ,
82
- two : 2 ,
83
- one_hundred : 100 ,
82
+ let x = Box :: new (GrossaStruct {
83
+ uno : 1 ,
84
+ due : 2 ,
85
+ cento : 100 ,
84
86
});
85
87
86
- let y : Box <BigStruct > = box foo (x );
88
+ let y : Box <GrossaStruct > = box foo (x );
87
89
}
88
90
```
89
91
90
- This gives you flexibility without sacrificing performance .
92
+ Ciò fornisce flessibilità senza sacrificare le prestazioni .
91
93
92
- You may think that this gives us terrible performance: return a value and then
93
- immediately box it up ?! Isn't this pattern the worst of both worlds? Rust is
94
- smarter than that. There is no copy in this code. ` main ` allocates enough room
95
- for the ` box ` , passes a pointer to that memory into ` foo ` as ` x ` , and then
96
- ` foo ` writes the value straight into the ` Box<T> ` .
94
+ Si potrebbe pensare che così si otterrebbero prestazioni terribili:
95
+ restituire un valore e poi inscatolarlo immediatamente?! Questo pattern
96
+ non è il peggio di entrambi i mondi? Ma Rust è più scaltro.
97
+ In questo codice non si fanno copie. ` main ` alloca abbastanza spazio
98
+ per il ` box ` , passa a ` foo ` , col nome di ` x ` , l'indirizzo di tale spazio,
99
+ e poi ` foo ` scrive il valore direttamente nel ` Box<T> ` .
97
100
98
- This is important enough that it bears repeating: pointers are not for
99
- optimizing returning values from your code. Allow the caller to choose how they
100
- want to use your output.
101
+ Ciò è abbastanza importante che vale la pena ripeterlo: i puntatori
102
+ non sono da usare per ottimizzare la restituzione di valori
103
+ dal proprio codice. Si consenta al chiamante di scegliere come vuole usare
104
+ l'output della funzione.
0 commit comments