Use c"lit" for CStrings without unwrap
This commit is contained in:
parent
3bff51ea91
commit
eadea7764e
14 changed files with 38 additions and 50 deletions
|
@ -15,7 +15,7 @@ unsafe { printf(); } // error!
|
|||
Using this declaration, it must be called with at least one argument, so
|
||||
simply calling `printf()` is invalid. But the following uses are allowed:
|
||||
|
||||
```
|
||||
```rust,edition2021
|
||||
# use std::os::raw::{c_char, c_int};
|
||||
# #[cfg_attr(all(windows, target_env = "msvc"),
|
||||
# link(name = "legacy_stdio_definitions",
|
||||
|
@ -23,16 +23,11 @@ simply calling `printf()` is invalid. But the following uses are allowed:
|
|||
# extern "C" { fn printf(_: *const c_char, ...) -> c_int; }
|
||||
# fn main() {
|
||||
unsafe {
|
||||
use std::ffi::CString;
|
||||
printf(c"test\n".as_ptr());
|
||||
|
||||
let fmt = CString::new("test\n").unwrap();
|
||||
printf(fmt.as_ptr());
|
||||
printf(c"number = %d\n".as_ptr(), 3);
|
||||
|
||||
let fmt = CString::new("number = %d\n").unwrap();
|
||||
printf(fmt.as_ptr(), 3);
|
||||
|
||||
let fmt = CString::new("%d, %d\n").unwrap();
|
||||
printf(fmt.as_ptr(), 10, 5);
|
||||
printf(c"%d, %d\n".as_ptr(), 10, 5);
|
||||
}
|
||||
# }
|
||||
```
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue