1
Fork 0

Use c"lit" for CStrings without unwrap

This commit is contained in:
Kornel 2024-11-30 20:32:00 +00:00
parent 3bff51ea91
commit eadea7764e
No known key found for this signature in database
14 changed files with 38 additions and 50 deletions

View file

@ -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);
}
# }
```