Skip to content

Commit 0b8dd9f

Browse files
committed
Finito "box syntax and patterns"
1 parent 178708f commit 0b8dd9f

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

box-syntax-and-patterns.md

+51-47
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
% Box Syntax and Patterns
1+
% La sintassi e i pattern di Box
22

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:
78

89
```rust
910
#![feature(box_syntax, box_patterns)]
@@ -12,89 +13,92 @@ fn main() {
1213
let b = Some(box 5);
1314
match b {
1415
Some(box n) if n < 0 => {
15-
println!("Box contains negative number {}", n);
16+
println!("Il box contiene il numero negativo {}", n);
1617
},
1718
Some(box n) if n >= 0 => {
18-
println!("Box contains non-negative number {}", n);
19+
println!("Il box contiene il numero non negativo {}", n);
1920
},
2021
None => {
21-
println!("No box");
22+
println!("Nessun box");
2223
},
2324
_ => unreachable!()
2425
}
2526
}
2627
```
2728

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.
3133

32-
# Returning Pointers
34+
# Restituire puntatori
3335

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:
3638

3739
```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,
4345
}
4446

45-
fn foo(x: Box<BigStruct>) -> Box<BigStruct> {
47+
fn foo(x: Box<GrossaStruct>) -> Box<GrossaStruct> {
4648
Box::new(*x)
4749
}
4850

4951
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,
5456
});
5557

5658
let y = foo(x);
5759
}
5860
```
5961

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`.
6264

63-
This is an antipattern in Rust. Instead, write this:
65+
Questo in Rust è un antipattern. Invece, si scrive così:
6466

6567
```rust
6668
#![feature(box_syntax)]
6769

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,
7375
}
7476

75-
fn foo(x: Box<BigStruct>) -> BigStruct {
77+
fn foo(x: Box<GrossaStruct>) -> GrossaStruct {
7678
*x
7779
}
7880

7981
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,
8486
});
8587

86-
let y: Box<BigStruct> = box foo(x);
88+
let y: Box<GrossaStruct> = box foo(x);
8789
}
8890
```
8991

90-
This gives you flexibility without sacrificing performance.
92+
Ciò fornisce flessibilità senza sacrificare le prestazioni.
9193

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>`.
97100

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

Comments
 (0)