-
Notifications
You must be signed in to change notification settings - Fork 0
/
never-type.html
154 lines (148 loc) · 3.59 KB
/
never-type.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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' />
<title>Big</title>
<link href='big.css' rel='stylesheet' type='text/css' />
<link href='styles/vs2015.css' rel='stylesheet' type='text/css' />
<style>
.new-shiny { background: #aaaaaa; }
:-webkit-full-screen {
width: 100%;
height: 100%;
}
.light {
background: #f8f8f8;
color: #4C5859;
}
.light em {
color: #26CC8F;
}
.light a {
color: #05A8AA;
}
.light a:hover {
color: #058A8C;
}
code {
padding: 20px 100px 30px 50px !important;
font-family: "hermit", monospace;
}
/* .inline-block {
display: inline-block;
} */
</style>
<script>BIG_ASPECT_RATIO=1.6;</script>
<script src='big.js'></script>
<script src='highlight.pack.js'></script>
<script>hljs.initHighlightingOnLoad();</script>
<script>
// document.addEventListener('keydown', ({ key }) => {
// if (key !== 'f') return
//
// console.log('document.fullscreenEnabled', document.fullscreenEnabled)
//
// if (document.fullscreenElement) {
// document.exitFullscreen()
// } else {
// document.documentElement.requestFullscreen()
// }
// })
</script>
</head>
<body id='body' class='light'>
<div>
<center><em>(Im)practical Rust:</em> <br/>Never Type</center>
<notes>Rust's strong type system allows for some uncommon functionality.</notes>
</div>
<div>
Representing the impossible
<br/>
<notes>Code paths which cannot occur</notes>
</div>
<div>
<pre><code class="rust">
fn main() -> Result<(), Error> {
loop {
// listen to network I/O for example
// only exits loop if there is an error
}
// Is unit the correct representation?
Ok(())
}
</code></pre>
</div>
<div>
Unit Type: <code>()</code>
<br/>
<br/>
<ul>
<li><em>Exists at runtime</em></li>
<li>Has only one value</li>
<li><em>Represents absence of data</em></li>
</ul>
<notes>For example, functions without a return type implicitly return unit.</notes>
</div>
<div>
Never Type: <code>!</code>
<br/>
<br/>
<li><em>Only exists at compile time</em></li>
<li>Not representable in executable code</li>
<li><em>Represents impossibility</em></li>
<notes></notes>
</div>
<div>
Common execution impossibilities
<br/>
<br/>
<li><code>return</code></li>
<li><code>continue</code></li>
<li><code>break</code></li>
<notes>These keywords all exit execution to an outer scope, meaning code in the same scope following them can never execute, therefor they implicitly return never.</notes>
</div>
<div>
Representing the impossible (actually)
<br/>
</div>
<div>
<pre><code class="rust">
fn main() -> Result<!, Error> {
loop {
// listen to network I/O for example
// only exits loop if there is an error
}
}
</code></pre>
</div>
<div>
This is a nightly-only experimental API.
<br/>
<pre><code class="rust">
#![feature(never_type)]
let x: ! = {
return 123
};
</code></pre>
</div>
<div>
Already representable in today's Rust (sort of).
</div>
<div>
<code class="rust">
std::convert::Infallible
</code>
<br/>
<br/>
<pre><code class="rust">
pub enum Infallible {}
</code></pre>
</div>
<div>
<pre><code class="rust">
use std::convert::Infallible as Never;
</code></pre>
</div>
</body>
</html>