]> git.lizzy.rs Git - rust.git/blob - src/docs/strlen_on_c_strings.txt
Auto merge of #9425 - kraktus:patch-1, r=xFrednet
[rust.git] / src / docs / strlen_on_c_strings.txt
1 ### What it does
2 Checks for usage of `libc::strlen` on a `CString` or `CStr` value,
3 and suggest calling `as_bytes().len()` or `to_bytes().len()` respectively instead.
4
5 ### Why is this bad?
6 This avoids calling an unsafe `libc` function.
7 Currently, it also avoids calculating the length.
8
9 ### Example
10 ```
11 use std::ffi::CString;
12 let cstring = CString::new("foo").expect("CString::new failed");
13 let len = unsafe { libc::strlen(cstring.as_ptr()) };
14 ```
15 Use instead:
16 ```
17 use std::ffi::CString;
18 let cstring = CString::new("foo").expect("CString::new failed");
19 let len = cstring.as_bytes().len();
20 ```