File tree 3 files changed +51
-4
lines changed
3 files changed +51
-4
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,23 @@ appears to the reader as the initial idea but works with doc tests:
236
236
/// ```
237
237
```
238
238
239
+ As of version 1.34.0, one can also omit the ` fn main() ` , but you will have to
240
+ disambiguate the error type:
241
+
242
+ ``` ignore
243
+ /// ```
244
+ /// use std::io;
245
+ /// let mut input = String::new();
246
+ /// io::stdin().read_line(&mut input)?;
247
+ /// # Ok::<(), io:Error>(())
248
+ /// ```
249
+ ```
250
+
251
+ This is an unfortunate consequence of the ` ? ` operator adding an implicit
252
+ conversion, so type inference fails because the type is not unique. Please note
253
+ that you must write the ` (()) ` in one sequence without intermediate whitespace
254
+ so that rustdoc understands you want an implicit ` Result ` -returning function.
255
+
239
256
## Documenting macros
240
257
241
258
Here’s an example of documenting a macro:
Original file line number Diff line number Diff line change @@ -470,13 +470,19 @@ pub fn make_test(s: &str,
470
470
}
471
471
}
472
472
473
- if dont_insert_main || already_has_main {
473
+ // FIXME: This code cannot yet handle no_std test cases yet
474
+ if dont_insert_main || already_has_main || prog. contains ( "![no_std]" ) {
474
475
prog. push_str ( everything_else) ;
475
476
} else {
476
- prog. push_str ( "fn main() {\n " ) ;
477
+ let returns_result = everything_else. trim_end ( ) . ends_with ( "(())" ) ;
478
+ let ( main_pre, main_post) = if returns_result {
479
+ ( "fn main() { fn _inner() -> Result<(), impl core::fmt::Debug> {" ,
480
+ "}\n _inner().unwrap() }" )
481
+ } else {
482
+ ( "fn main() {\n " , "\n }" )
483
+ } ;
484
+ prog. extend ( [ main_pre, everything_else, main_post] . iter ( ) . cloned ( ) ) ;
477
485
line_offset += 1 ;
478
- prog. push_str ( everything_else) ;
479
- prog. push_str ( "\n }" ) ;
480
486
}
481
487
482
488
debug ! ( "final doctest:\n {}" , prog) ;
Original file line number Diff line number Diff line change
1
+ // compile-flags:--test
2
+
3
+ /// A check of using various process termination strategies
4
+ ///
5
+ /// # Examples
6
+ ///
7
+ /// ```rust
8
+ /// assert!(true); // this returns `()`, all is well
9
+ /// ```
10
+ ///
11
+ /// You can also simply return `Ok(())`, but you'll need to disambiguate the
12
+ /// type using turbofish, because we cannot infer the type:
13
+ ///
14
+ /// ```rust
15
+ /// Ok::<(), &'static str>(())
16
+ /// ```
17
+ ///
18
+ /// You can err with anything that implements `Debug`:
19
+ ///
20
+ /// ```rust,should_panic
21
+ /// Err("This is returned from `main`, leading to panic")?;
22
+ /// Ok::<(), &'static str>(())
23
+ /// ```
24
+ pub fn check_process_termination ( ) { }
You can’t perform that action at this time.
0 commit comments