File tree Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -162,11 +162,15 @@ Rust 的 core 库内建了以一系列帮助实现显示字符的基本 Trait
162
162
163
163
如果你觉得理解 Rust 宏有困难,把它当成黑盒就好!
164
164
165
+ 学习rust宏的参考链接: `The Little Book of Rust Macros <https://veykril.github.io/tlborm/introduction.html >`_
166
+
165
167
166
168
首先封装一下对 ``SYSCALL_WRITE `` 系统调用。
167
169
168
170
.. code-block :: rust
169
171
172
+ // os/src/main.rs
173
+
170
174
const SYSCALL_WRITE: usize = 64;
171
175
172
176
pub fn sys_write(fd: usize, buffer: &[u8]) -> isize {
@@ -178,6 +182,8 @@ Rust 的 core 库内建了以一系列帮助实现显示字符的基本 Trait
178
182
179
183
.. code-block :: rust
180
184
185
+ // os/src/console.rs
186
+
181
187
struct Stdout;
182
188
183
189
impl Write for Stdout {
@@ -196,20 +202,31 @@ Rust 的 core 库内建了以一系列帮助实现显示字符的基本 Trait
196
202
197
203
.. code-block :: rust
198
204
199
- #[macro_export]
205
+ // os/src/console.rs
206
+
200
207
macro_rules! print {
201
208
($fmt: literal $(, $($arg: tt)+)?) => {
202
209
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
203
210
}
204
211
}
205
212
206
- #[macro_export]
207
213
macro_rules! println {
208
214
($fmt: literal $(, $($arg: tt)+)?) => {
209
- print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
215
+ $crate::console:: print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
210
216
}
211
217
}
212
218
219
+ // os/src/main.rs
220
+
221
+ #![no_std]
222
+ #![no_main]
223
+
224
+ #[macro_use]
225
+ mod console;
226
+ mod lang_items;
227
+
228
+ ...
229
+
213
230
接下来,我们调整一下应用程序,让它发出显示字符串和退出的请求:
214
231
215
232
.. code-block :: rust
You can’t perform that action at this time.
0 commit comments