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
// Copyright © 2017 Bart Massey
// [This program is licensed under the "MIT License"]
// Please see the file LICENSE in the source
// distribution of this software for license terms.
/*!
This crate provides macros for easy non-newline-terminated
flushed printing, and for input line reading. These macros
are intended for new Rust users and for folks who need no
more for simple applications.
# Example
Here's an example adapted from the "Guessing Game" example
in [*The Rust Programming
Language*](https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html).
```no_run
use std::cmp::Ordering;
use prompted::input;
fn main() {
println!("Guess the number!");
let n = 100;
let secret_number = 37;
loop {
let guess = input!("Please input your guess (1-{}): ", n);
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {}", guess);
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}
```
*/
use ;
/// Flush standard output. Intended primarily for use by
/// macros.
///
/// # Panics
///
/// Panics if writing to [stdout] fails.
/// Flush standard error. Intended primarily for use by
/// macros.
///
/// # Panics
///
/// Panics if writing to [stderr] fails.
/// Read a line from standard input. Removes a trailing
/// newline if present. Intended primarily for use by
/// macros.
///
/// # Panics
///
/// Panics if reading from [stdin] fails.
/// Same functionality as [print!()] except that [stdout]
/// is flushed at the end.
///
/// As with [print!()], the multi-argument form of this
/// macro supports the [format!()] syntax for building a
/// string. With no arguments, only the flush is performed.
///
/// # Panics
///
/// Panics if writing to [stdout] fails.
///
/// # Examples
///
/// ```
/// # use prompted::prompt;
/// # pub fn main() {
/// prompt!();
/// prompt!("Pick a number between 1 and {}: ", 10);
/// # }
/// ```
/// Same functionality as [prompt!()] except using [stderr]
/// instead of [stdout].
///
/// # Panics
///
/// Panics if writing to [stderr] fails.
/// If a [format!()] describing a prompt is present, print it
/// on [stdout] and then flush. Then read a line from
/// [stdin] and return it after removing the line ending.
///
/// # Panics
///
/// Panics if reading from [stdin] or writing to [stdout] fails.
///
/// # Examples
///
/// ```
/// # use prompted::input;
/// # pub fn main() {
/// let m = 10;
/// let guess = input!("Pick a number between 1 and {}: ", m);
/// match guess.parse::<isize>() {
/// Ok(n) => if n >=1 && n <= m {
/// println!("Thank you for choosing {}", n)
/// } else {
/// println!("You failed arithmetic with {}", n)
/// },
/// Err(e) => println!("Not even a number. {}?", e)
/// }
/// # }
/// ```