]> git.lizzy.rs Git - rust.git/blob - src/docs/unsound_collection_transmute.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / unsound_collection_transmute.txt
1 ### What it does
2 Checks for transmutes between collections whose
3 types have different ABI, size or alignment.
4
5 ### Why is this bad?
6 This is undefined behavior.
7
8 ### Known problems
9 Currently, we cannot know whether a type is a
10 collection, so we just lint the ones that come with `std`.
11
12 ### Example
13 ```
14 // different size, therefore likely out-of-bounds memory access
15 // You absolutely do not want this in your code!
16 unsafe {
17     std::mem::transmute::<_, Vec<u32>>(vec![2_u16])
18 };
19 ```
20
21 You must always iterate, map and collect the values:
22
23 ```
24 vec![2_u16].into_iter().map(u32::from).collect::<Vec<_>>();
25 ```