We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent acef005 commit e333d15Copy full SHA for e333d15
idioms/on-stack-dyn-dispatch.md
@@ -9,14 +9,18 @@ below:
9
10
## Example
11
12
-```rust,ignore
13
-std::io::File;
+```rust
+use std::io;
14
+use std::fs;
15
+
16
+# fn main() -> Result<(), Box<dyn std::error::Error>> {
17
+# let arg = "-";
18
19
// These must live longer than `readable`, and thus are declared first:
20
let (mut stdin_read, mut file_read);
21
22
// We need to ascribe the type to get dynamic dispatch.
-let readable: &mut dyn io::Read = if arg == '-' {
23
+let readable: &mut dyn io::Read = if arg == "-" {
24
stdin_read = io::stdin();
25
&mut stdin_read
26
} else {
@@ -25,6 +29,9 @@ let readable: &mut dyn io::Read = if arg == '-' {
29
};
30
27
31
// Read from `readable` here.
32
33
+# Ok(())
34
+# }
28
35
```
36
37
## Motivation
0 commit comments