-
Notifications
You must be signed in to change notification settings - Fork 30
/
types-and-traits.rst
3360 lines (2430 loc) · 91.8 KB
/
types-and-traits.rst
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.. SPDX-License-Identifier: MIT OR Apache-2.0
SPDX-FileCopyrightText: The Ferrocene Developers
.. default-domain:: spec
.. _fls_vgb6ev541b2r:
Types and Traits
================
.. _fls_kwsBxMQNTRnL:
Types
-----
.. rubric:: Syntax
.. syntax::
TypeSpecification ::=
ImplTraitTypeSpecification
| TraitObjectTypeSpecification
| TypeSpecificationWithoutBounds
TypeSpecificationList ::=
TypeSpecification (, TypeSpecification)* $$,$$?
TypeSpecificationWithoutBounds ::=
ArrayTypeSpecification
| FunctionPointerTypeSpecification
| ImplTraitTypeSpecificationOneBound
| InferredType
| MacroInvocation
| NeverType
| ParenthesizedTypeSpecification
| QualifiedTypePath
| RawPointerTypeSpecification
| ReferenceTypeSpecification
| SliceTypeSpecification
| TraitObjectTypeSpecificationOneBound
| TupleTypeSpecification
| TypePath
TypeAscription ::=
$$:$$ TypeSpecification
.. rubric:: Legality Rules
:dp:`fls_4rhjpdu4zfqj`
A :t:`type` defines a set of :t:`[value]s` and a set of operations that act on
those :t:`[value]s`.
:dp:`fls_0yaYKnFrJkhG`
A :t:`local type` is a :t:`type` that is defined in the current :t:`crate`.
.. _fls_963gsjp2jas2:
Type Classification
-------------------
.. informational-section::
.. rubric:: Legality Rules
:dp:`fls_c4xe3pkn0n3o`
:t:`[Type]s` are organized in the following categories:
* :dp:`fls_69zyas59o8ff`
:t:`[Scalar type]s`
* :dp:`fls_65hcyqizo1da`
:c:`Bool` :t:`type`
* :dp:`fls_zge99l49az8w`
:c:`Char` :t:`type`
* :dp:`fls_vizoconv3ir`
:t:`[Numeric type]s`
* :dp:`fls_ne6bgnh1eyrj`
:t:`Floating-point type`
* :dp:`fls_jvj8l8366kl2`
:t:`Integer type`
* :dp:`fls_eek1jn1rwjh9`
:t:`[Sequence type]s`
* :dp:`fls_s0aduyvz4i7f`
:t:`[Array type]s`
* :dp:`fls_zb5e79ai7w5i`
:t:`[Slice type]s`
* :dp:`fls_yjp19vt46asy`
:c:`Str` :t:`type`
* :dp:`fls_xflj5df6upc7`
:t:`[Tuple type]s`
* :dp:`fls_u43jnp9jnw29`
:t:`[Abstract data type]s`
* :dp:`fls_lric8bf631nw`
:t:`[Enum type]s`
* :dp:`fls_98djh9avlqc0`
:t:`[Struct type]s`
* :dp:`fls_b3ymsm8dmo4`
:t:`[Union type]s`
* :dp:`fls_9x5atvhdq0j2`
:t:`[Function type]s`
* :dp:`fls_n5rgqgnxk9to`
:t:`[Closure type]s`
* :dp:`fls_s7ndqc5sizdy`
:t:`[Function item type]s`
* :dp:`fls_jrohsv7hx7yw`
:t:`[Indirection type]s`
* :dp:`fls_1kg1mknf4yx7`
:t:`[Function pointer type]s`
* :dp:`fls_bw8zutjcteki`
:t:`[Raw pointer type]s`
* :dp:`fls_nqezuc9u6wpn`
:t:`[Reference type]s`
* :dp:`fls_lh52q6f6snfh`
:t:`[Trait type]s`
* :dp:`fls_qqg0uixrd1a4`
:t:`[Impl trait type]s`
* :dp:`fls_b8ecqp2argmn`
:t:`[Trait object type]s`
* :dp:`fls_m5vtcars8aga`
Other :t:`[type]s`
* :dp:`fls_lw38557rqikt`
:t:`[Inferred type]s`
* :dp:`fls_jxn63ow9xby3`
:t:`Never type`
* :dp:`fls_a81tweobvm0p`
:t:`[Parenthesized type]s`
.. _fls_id66vnaqw0zt:
Scalar Types
------------
.. _fls_tiqp1gxf116z:
Bool Type
~~~~~~~~~
.. rubric:: Legality Rules
:dp:`fls_h5994su1yft3`
:c:`Bool` is a :t:`type` whose :t:`[value]s` denote the truth :t:`[value]s` of
logic and Boolean algebra.
:dp:`fls_v8atmrwz6wzk`
:t:`Type` :c:`bool` appears in the :t:`language prelude` under the name
``bool``.
:dp:`fls_iye7ho2ynyhn`
Boolean :t:`value` ``false`` has bit pattern ``0x00``. Boolean :t:`value`
``true`` has bit pattern ``0x01``.
:dp:`fls_7nd5tixyqir8`
The following operations are defined on :t:`type` :c:`bool`:
:dp:`fls_w2dzqq54fjhb`
**Logical not**
.. list-table::
* - :dp:`fls_ufmd38hi9t9y`
- **a**
- **!a**
* - :dp:`fls_5allcjkjnon2`
- ``true``
- ``false``
* - :dp:`fls_3bibysz95ktn`
- ``false``
- ``true``
:dp:`fls_fxq19dqtmifj`
**Logical and**
.. list-table::
* - :dp:`fls_drhpcwoblcux`
- **a**
- **b**
- **a & b**
* - :dp:`fls_v86qrsqcs3nd`
- ``true``
- ``true``
- ``true``
* - :dp:`fls_dd49lb2k3erc`
- ``true``
- ``false``
- ``false``
* - :dp:`fls_t6ef5x4x5poi`
- ``false``
- ``true``
- ``false``
* - :dp:`fls_kqtgjgn1hqrj`
- ``false``
- ``false``
- ``false``
:dp:`fls_ws15ilzf8n6z`
**Logical or**
.. list-table::
* - :dp:`fls_ni4mgq3mouek`
- **a**
- **b**
- **a | b**
* - :dp:`fls_6c9ax4qsr1gy`
- ``true``
- ``true``
- ``true``
* - :dp:`fls_sqcgvpr4egtx`
- ``true``
- ``false``
- ``true``
* - :dp:`fls_9ys0itbp4okd`
- ``false``
- ``true``
- ``true``
* - :dp:`fls_b46gbyid15zx`
- ``false``
- ``false``
- ``false``
:dp:`fls_f8ag276ecbze`
**Logical exclusive or (xor)**
.. list-table::
* - :dp:`fls_twwjcrcfirdi`
- **a**
- **b**
- **a ^ b**
* - :dp:`fls_wovu7330vdrq`
- ``true``
- ``true``
- ``false``
* - :dp:`fls_7xopdco6iy74`
- ``true``
- ``false``
- ``true``
* - :dp:`fls_nb5cb6en2p5w`
- ``false``
- ``true``
- ``true``
* - :dp:`fls_gd28wfcfs2pv`
- ``false``
- ``false``
- ``false``
:dp:`fls_67a7p57nzbul`
**Equality**
.. list-table::
* - :dp:`fls_cq0qunw51m94`
- **a**
- **b**
- **a == b**
* - :dp:`fls_o1e4tnh7v3db`
- ``true``
- ``true``
- ``true``
* - :dp:`fls_6vnv3ygisjr`
- ``true``
- ``false``
- ``false``
* - :dp:`fls_s6m9abmmtc9i`
- ``false``
- ``true``
- ``false``
* - :dp:`fls_s19vu65z96y5`
- ``false``
- ``false``
- ``true``
:dp:`fls_2d4aqspw0wlt`
**Greater than**
.. list-table::
* - :dp:`fls_msjo2zd67zn1`
- **a**
- **b**
- **a > b**
* - :dp:`fls_w1oti03tm1y6`
- ``true``
- ``true``
- ``false``
* - :dp:`fls_9gqd7eevbknt`
- ``true``
- ``false``
- ``true``
* - :dp:`fls_r4o2rmhqg4br`
- ``false``
- ``true``
- ``false``
* - :dp:`fls_1n7p6ij1dpm`
- ``false``
- ``false``
- ``false``
:dp:`fls_4x27kfiodb8`
Operation ``a != b`` is equivalent to ``!(a == b)``.
:dp:`fls_me6bf9m2ypt`
Operation ``a >= b`` is equivalent to ``a == b | a > b``.
:dp:`fls_2j659ns8wop4`
Operation ``a < b`` is equivalent to ``!(a >= b)``.
:dp:`fls_d09l2rl0161l`
Operation ``a <= b`` is equivalent to ``a == b | a < b``.
.. rubric:: Undefined Behavior
:dp:`fls_2sd39mj05mb9`
It is a :t:`validity invariant` for a :t:`value` of :t:`type` :c:`bool` to have
a bit pattern of ``0x00`` and ``0x01``.
.. _fls_wrvjizrqf3po:
Char Type
~~~~~~~~~
.. rubric:: Legality Rules
:dp:`fls_vnwbs0exbwcn`
:c:`Char` is a :t:`type` whose :t:`[value]s` are represented as a 32-bit
unsigned word in the 0x000 - 0xD7FF or the 0xE000 - 0x10FFFF inclusive ranges
of :t:`Unicode`.
.. rubric:: Undefined Behavior
:dp:`fls_juysxea25owj`
It is a :t:`validity invariant` for a :t:`value` of :t:`type` :c:`char` to be
inside the 0x000 - 0xD7FF or the 0xE000 - 0x10FFFF inclusive ranges of
:t:`Unicode`.
.. _fls_qwljwqr07slp:
Numeric Types
~~~~~~~~~~~~~
.. _fls_b4xporvr64s:
Floating Point Types
^^^^^^^^^^^^^^^^^^^^
.. rubric:: Legality Rules
:dp:`fls_30yny2xb9b6b`
:t:`Type` :c:`f32` is equivalent to the IEEE 754-2008 binary32 :t:`type`.
:dp:`fls_yqflrq9s6p6n`
:t:`Type` :c:`f64` is equivalent to the IEEE 754-2008 binary64 :t:`type`.
.. _fls_3qnpv2z7yjil:
Integer Types
^^^^^^^^^^^^^
.. rubric:: Legality Rules
:dp:`fls_cokwseo3nnr`
:t:`[Unsigned integer type]s` define the following inclusive ranges over the
domain of whole numbers:
.. list-table::
* - :dp:`fls_vk1skn6ek36u`
- **Type**
- **Minimum**
- **Maximum**
* - :dp:`fls_iikexw8ps6mk`
- :c:`u8`
- 0
- 2\ :sup:`8` - 1
* - :dp:`fls_cavasxxlgs7g`
- :c:`u16`
- 0
- 2\ :sup:`16` - 1
* - :dp:`fls_7sx92xsjx3pl`
- :c:`u32`
- 0
- 2\ :sup:`32` - 1
* - :dp:`fls_q9f95uet7gq4`
- :c:`u64`
- 0
- 2\ :sup:`64` - 1
* - :dp:`fls_yjb3kzijd19v`
- :c:`u128`
- 0
- 2\ :sup:`128` - 1
:dp:`fls_75lntwhg20l`
:t:`Type` :c:`usize` has the same number of bits as the platform's
:t:`pointer type`, and is at least 16-bits wide.
:dp:`fls_p2shoji3xg5a`
:t:`[Signed integer type]s` define the following inclusive ranges over the
domain of whole numbers:
.. list-table::
* - :dp:`fls_fsyt05u9y4sl`
- **Type**
- **Minimum**
- **Maximum**
* - :dp:`fls_p9ffvtajr832`
- :c:`i8`
- \- (2\ :sup:`7`)
- 2\ :sup:`7` - 1
* - :dp:`fls_j6xan9f8udw7`
- :c:`i16`
- \- (2\ :sup:`15`)
- 2\ :sup:`15` - 1
* - :dp:`fls_4t39p3ibkzu7`
- :c:`i32`
- \- (2\ :sup:`31`)
- 2\ :sup:`31` - 1
* - :dp:`fls_egfoxke0lzje`
- :c:`i64`
- \- (2\ :sup:`63`)
- 2\ :sup:`63` - 1
* - :dp:`fls_4c4qpel1tbqs`
- :c:`i128`
- \- (2\ :sup:`127`)
- 2\ :sup:`127` - 1
:dp:`fls_t9oyfmgqka6u`
:t:`Type` :c:`isize` has the same number of bits as the platform's
:t:`pointer type`, and is at least 16-bits wide.
.. _fls_fbchw64p6n2x:
Sequence Types
--------------
.. _fls_uj0kpjwyld60:
Array Types
~~~~~~~~~~~
.. rubric:: Syntax
.. syntax::
ArrayTypeSpecification ::=
$$[$$ ElementType $$;$$ SizeOperand $$]$$
ElementType ::=
TypeSpecification
.. rubric:: Legality Rules
:dp:`fls_fx7b3qv3ghca`
An :t:`array type` is a :t:`sequence type` that represents a fixed sequence
of elements.
:dp:`fls_pkts1p2dnxo`
The :t:`element type` shall be a :t:`fixed sized type`.
:dp:`fls_imr2jx6cbuzq`
The :t:`size operand` shall be a :t:`constant expression`.
:dp:`fls_r8nqxry2dlww`
The :t:`type` of the :t:`size operand` is :t:`type` :c:`usize`.
.. rubric:: Examples
:dp:`fls_9vjijqi9w8wn`
An array type in the context of a let statement:
.. code-block:: rust
let array: [i32; 3] = [1, 2, 3];
.. _fls_vpbikb73dw4k:
Slice Types
~~~~~~~~~~~
.. rubric:: Syntax
.. syntax::
SliceTypeSpecification ::=
$$[$$ ElementType $$]$$
.. rubric:: Legality Rules
:dp:`fls_ftvua2hlvr08`
A :t:`slice type` is a :t:`sequence type` that provides a view into a sequence
of elements.
:dp:`fls_acgtczhk8ci0`
The :t:`element type` shall be a :t:`fixed sized type`.
:dp:`fls_5gl67ftc3m21`
A :t:`slice type` is a :t:`dynamically sized type`.
.. rubric:: Examples
:dp:`fls_nsny832ap4v1`
A slice type in the context of a let statement:
.. code-block:: rust
let array: [i32; 3] = [1, 2, 3];
let slice: &[i32] = &array[0..1];
.. _fls_4agmmu5al6gt:
Str Type
~~~~~~~~
.. rubric:: Legality Rules
:dp:`fls_wlnoq1qoq2kr`
:c:`Str` is a :t:`sequence type` that represents a :t:`slice` of 8-bit unsigned
bytes.
:dp:`fls_1xa6fas6laha`
:t:`Type` :c:`str` is a :t:`dynamically sized type`.
:dp:`fls_yu7r2077n9m7`
A :t:`value` of :t:`type` :c:`str` shall denote a valid UTF-8 sequence of
characters.
.. rubric:: Undefined Behavior
:dp:`fls_wacoqrtzvrwu`
It is a :t:`safety invariant` for a :t:`value` of :t:`type` :c:`str` to denote
a valid UTF-8 sequence of characters.
.. _fls_4ckl3n2ko3i4:
Tuple Types
~~~~~~~~~~~
.. rubric:: Syntax
.. syntax::
TupleTypeSpecification ::=
$$($$ TupleFieldList? $$)$$
TupleFieldList ::=
TupleField (, TupleField)* ,?
TupleField ::=
TypeSpecification
.. rubric:: Legality Rules
:dp:`fls_bn7wmf681ngt`
A :t:`tuple type` is a :t:`sequence type` that represents a heterogeneous list
of other :t:`[type]s`.
:dp:`fls_s9a36zsrfqew`
If the :t:`type` of a :t:`tuple field` is a :t:`dynamically-sized type`, then
the :t:`tuple field` shall be the last :t:`tuple field` in the
:s:`TupleFieldList`.
.. rubric:: Examples
.. code-block:: rust
()
(char,)
(i32, f64, Vec<String>)
.. _fls_wdec78luqh5b:
Abstract Data Types
-------------------
.. _fls_szibmtfv117b:
Enum Types
~~~~~~~~~~
.. rubric:: Syntax
.. syntax::
EnumDeclaration ::=
$$enum$$ Name GenericParameterList? WhereClause? $${$$ EnumVariantList? $$}$$
EnumVariantList ::=
EnumVariant ($$,$$ EnumVariant)* $$,$$?
EnumVariant ::=
OuterAttributeOrDoc* VisibilityModifier? Name EnumVariantKind?
EnumVariantKind ::=
DiscriminantInitializer
| RecordStructFieldList
| TupleStructFieldList
DiscriminantInitializer ::=
$$=$$ Expression
.. rubric:: Legality Rules
:dp:`fls_gbdd37seqoab`
An :t:`enum type` is an :t:`abstract data type` that contains
:t:`[enum variant]s`.
:dp:`fls_il9a1olqmu38`
A :t:`zero-variant enum type` has no :t:`[value]s`.
:dp:`fls_wQTFwl88VujQ`
An :t:`enum variant` is a :t:`construct` that declares one of the
possible variations of an :t:`enum`.
:dp:`fls_g5qle7xzaoif`
The :t:`name` of an :t:`enum variant` shall be unique within the related
:s:`EnumDeclaration`.
:dp:`fls_t4yeovFm83Wo`
A :t:`discriminant` is an opaque integer that identifies an :t:`enum variant`.
:dp:`fls_hp5frc752dam`
A :t:`discriminant initializer` shall be specified only when all :t:`[enum
variant]s` appear without an :s:`EnumVariantKind`.
:dp:`fls_pijczoq4k9ij`
The :t:`type` of the :t:`expression` of a :t:`discriminant initializer` shall
be either:
* :dp:`fls_x7nh42on06bg`
The :t:`type` of the :t:`primitive representation` specified by :t:`attribute`
:c:`repr`, or
* :dp:`fls_duqbzvpuehvv`
:t:`Type` :c:`isize`.
:dp:`fls_ly183pj4fkgh`
The :t:`value` of the :t:`expression` of a :t:`discriminant initializer` shall
be a :t:`constant expression`.
:dp:`fls_w7sggezgq9o4`
The :t:`value` of a :t:`discriminant` of an :t:`enum variant` is determined
as follows:
#. :dp:`fls_93l5o6qar5p2`
If the :t:`enum variant` contains a :t:`discriminant initializer`, then the
:t:`value` is the value of its :t:`expression`.
#. :dp:`fls_t36rk3wikq28`
Otherwise, if the :t:`enum variant` is the first :t:`enum variant` in the
:s:`EnumVariantList`, then the :t:`value` is zero.
#. :dp:`fls_8ajw5trd23wi`
Otherwise the :t:`value` is one greater than the :t:`value` of the
:t:`discriminant` of the previous :t:`enum variant`.
:dp:`fls_w9xj26ej869w`
It is a static error if two :t:`[enum variant]s` have :t:`[discriminant]s`
with the same :t:`value`.
:dp:`fls_wqbuof7kxsrg`
It is a static error if the :t:`value` of a :t:`discriminant` exceeds the
maximum :t:`value` of the :t:`type` of the :t:`expression` of a :t:`discriminant
initializer`.
.. rubric:: Undefined Behavior
:dp:`fls_f046du2fkgr6`
It is a :t:`validity invariant` for a :t:`value` of an :t:`enum type` to have a
:t:`discriminant` specified by the :t:`enum type`.
.. rubric:: Examples
.. code-block:: rust
enum ZeroVariantEnumType {}
enum Animal {
Cat,
Dog(String),
Otter { name: String, weight: f64, age: u8 }
}
enum Discriminants {
First, // The discriminant is 0.
Second, // The discriminant is 1.
Third = 12, // The discriminant is 12.
Fourth, // The discriminant is 13.
Fifth = 34, // The discriminant is 34.
Sixth // The discriminant is 35.
}
.. _fls_9ucqbbd0s2yo:
Struct Types
~~~~~~~~~~~~
.. rubric:: Syntax
.. syntax::
StructDeclaration ::=
RecordStructDeclaration
| TupleStructDeclaration
| UnitStructDeclaration
RecordStructDeclaration ::=
$$struct$$ Name GenericParameterList? WhereClause? RecordStructFieldList
RecordStructFieldList ::=
$${$$ (RecordStructField ($$,$$ RecordStructField)* $$,$$?)? $$}$$
RecordStructField ::=
OuterAttributeOrDoc* VisibilityModifier? Name TypeAscription
TupleStructDeclaration ::=
$$struct$$ Name GenericParameterList? TupleStructFieldList WhereClause? $$;$$
TupleStructFieldList ::=
$$($$ (TupleStructField ($$,$$ TupleStructField)* $$,$$?)? $$)$$
TupleStructField ::=
OuterAttributeOrDoc* VisibilityModifier? TypeSpecification
UnitStructDeclaration ::=
$$struct$$ Name GenericParameterList? WhereClause? $$;$$
.. rubric:: Legality Rules
:dp:`fls_g1azfj548136`
A :t:`struct type` is an :t:`abstract data type` that is a product of other
:t:`[type]s`.
:dp:`fls_r885av95eivp`
The :t:`name` of a :t:`record struct field` shall be unique within the
related :s:`RecordStructDeclaration`.
:dp:`fls_auurdv1zvzb`
If the :t:`type` of a :t:`record struct field` is a :t:`dynamically sized type`,
then the :t:`record struct field` shall be the last :t:`record struct field` in
the :s:`RecordStructFieldList`.
:dp:`fls_vce7w0904du5`
If the :t:`type` of a :t:`tuple struct field` is a :t:`dynamically sized type`,
then the :t:`tuple struct field` shall be the last :t:`tuple struct field` in
the :s:`TupleStructFieldList`.
.. rubric:: Examples
.. code-block:: rust
struct UnitStruct;
struct AnimalRecordStruct {
name: String,
weight: f64,
age: u8
}
struct AnimalTupleStruct (
String,
f64,
u8
);
.. _fls_fmdn7n7s413d:
Union Types
~~~~~~~~~~~
.. rubric:: Syntax
.. syntax::
UnionDeclaration ::=
$$union$$ Name GenericParameterList? WhereClause? RecordStructFieldList
.. rubric:: Legality Rules
:dp:`fls_nskmnzq95yqm`
A :t:`union type` is an :t:`abstract data type` that is a sum of other
:t:`types`.
:dp:`fls_1caus8ybmfli`
The :t:`name` of a :t:`union field` shall be unique within the related
:s:`RecordStructDeclaration`.
:dp:`fls_ZJG2Q6lJYXhY`
The :t:`type` of a :t:`union field` shall be either:
* :dp:`fls_hLTnHnZuaHve`
A :t:`copy type`, or
* :dp:`fls_JWgSckDtN13c`
A :t:`mutable reference type`, or
* :dp:`fls_sXZknxozJxtC`
:std:`core::mem::ManuallyDrop`, or
* :dp:`fls_vgNK01SXacnx`
A :t:`tuple type` whose :t:`[tuple field]s`' :t:`[type]s` are all valid
:t:`union field` :t:`[type]s`, or
* :dp:`fls_bQhh3zHAKjSu`
An :t:`array type` whose :t:`element type` is a valid :t:`union field`
:t:`[type]s`.
.. rubric:: Examples
.. code-block:: rust
union LeafNode {
int: i32,
float: f32,
double: f64
}
.. _fls_hbbek3z4wtcs:
Function Types
--------------
.. _fls_xd2oxlebhs14:
Closure Types
~~~~~~~~~~~~~
.. rubric:: Legality Rules
:dp:`fls_bsykgnbatpmi`
A :t:`closure type` is a unique anonymous :t:`function type` that encapsulates
all :t:`[capture target]s` of a :t:`closure expression`.
:dp:`fls_zfj4l8bigdg0`
A :t:`closure type` implements the :std:`core::ops::FnOnce` :t:`trait`.
:dp:`fls_bn0ueivujnqk`
A :t:`closure type` that does not move out its :t:`[capture target]s`
implements the :std:`core::ops::FnMut` :t:`trait`.
:dp:`fls_u01kt5glbuz8`
A :t:`closure type` that does not move out or mutate its :t:`[capture target]s`
implements the :std:`core::ops::Fn` :t:`trait`.
:dp:`fls_3jeootwe6ucu`
A :t:`closure type` that does not encapsulate :t:`[capture target]s` is
coercible to a :t:`function pointer type`.
:dp:`fls_63jqtyw0rz8c`
A :t:`closure type` implicitly implements the :std:`core::marker::Copy`
:t:`trait` if all the :t:`[type]s` of the :t:`[value]s` of the
:t:`capturing environment` implement the :std:`core::marker::Copy` :t:`trait`.
:dp:`fls_3c4g9njja5s5`
A :t:`closure type` implicitly implements the :std:`core::clone::Clone`
:t:`trait` if all the :t:`[type]s` of the :t:`[value]s` of the
:t:`capturing environment` implement the :std:`core::clone::Clone` :t:`trait`.
:dp:`fls_2nuhy0ujgq18`
A :t:`closure type` implicitly implements the :std:`core::marker::Send`
:t:`trait` if all the :t:`[type]s` of the :t:`[value]s` of the
:t:`capturing environment` implement the :std:`core::marker::Send` :t:`trait`.
:dp:`fls_5jh07heok8sy`
A :t:`closure type` implicitly implements the :std:`core::marker::Sync`
:t:`trait` if all the :t:`[type]s` of the :t:`[value]s` of the :t:`capturing
environment` implement the :std:`core::marker::Send` :t:`trait`.
.. _fls_airvr79xkcag:
Function Item Types
~~~~~~~~~~~~~~~~~~~
.. rubric:: Legality Rules
:dp:`fls_t24iojx7yc23`
A :t:`function item type` is a unique anonymous :t:`function type` that
identifies a :t:`function`.
:dp:`fls_sas3ahcshnrh`
An :t:`external function item type` is a :t:`function item type` where the
related :t:`function` is an :t:`external function`.
:dp:`fls_liwnzwu1el1i`
An :t:`unsafe function item type` is a :t:`function item type` where the related
:t:`function` is an :t:`unsafe function`.
:dp:`fls_e9x4f7qxvvjv`
A :t:`function item type` is coercible to a :t:`function pointer type`.
:dp:`fls_1941wid94hlg`
A :t:`function item type` implements the :std:`core::clone::Clone` :t:`trait`,
the :std:`core::marker::Copy` :t:`trait`, the :std:`core::ops::Fn` :t:`trait`,
the :std:`core::ops::FnMut` :t:`trait`, the :std:`core::ops::FnOnce` :t:`trait`,
the :std:`core::marker::Send` :t:`trait`, and the :std:`core::marker::Sync`
:t:`trait`.
.. _fls_3i4ou0dq64ny:
Indirection Types
-----------------
.. _fls_xztr1kebz8bo:
Function Pointer Types
~~~~~~~~~~~~~~~~~~~~~~
.. rubric:: Syntax
.. syntax::
FunctionPointerTypeSpecification ::=
ForGenericParameterList? FunctionPointerTypeQualifierList $$fn$$
$$($$ FunctionPointerTypeParameterList? $$)$$ ReturnTypeWithoutBounds?
FunctionPointerTypeQualifierList ::=
$$unsafe$$? AbiSpecification?
FunctionPointerTypeParameterList ::=
FunctionPointerTypeParameter ($$,$$ FunctionPointerTypeParameter)*
($$,$$ VariadicPart | $$,$$?)
VariadicPart ::=
OuterAttributeOrDoc* $$...$$
FunctionPointerTypeParameter ::=
OuterAttributeOrDoc* (IdentifierOrUnderscore $$:$$)? TypeSpecification
.. rubric:: Legality Rules
:dp:`fls_v2wrytr3t04h`
A :t:`function pointer type` is an :t:`indirection type` that refers to a
:t:`function`.
:dp:`fls_5dd7icjcl3nt`
An :t:`unsafe function pointer type` is a function pointer type subject to
:t:`keyword` ``unsafe``.
:dp:`fls_B0SMXRqQMS1E`
A :t:`variadic part` indicates the presence of :t:`C`-like optional
parameters.
:dp:`fls_hbn1l42xmr3h`
A :t:`variadic part` shall be specified only when the :t:`ABI` of the
:t:`function pointer type` is either ``extern "C"`` or ``extern "cdecl"``.
.. rubric:: Undefined Behavior
:dp:`fls_52thmi9hnoks`
It is a :t:`validity invariant` for a :t:`value` of a :t:`function pointer type`
to be not :c:`null`.
.. rubric:: Examples
.. code-block:: rust
unsafe extern "C" fn (value: i32, ...) -> f64
.. _fls_ppd1xwve3tr7:
Raw Pointer Types
~~~~~~~~~~~~~~~~~
.. rubric:: Syntax
.. syntax::
RawPointerTypeSpecification ::=
$$*$$ ($$const$$ | $$mut$$) TypeSpecificationWithoutBounds
.. rubric:: Legality Rules
:dp:`fls_rpbhr0xukbx9`