pub struct CString { /* fields omitted */ }
A type representing an owned C-compatible string
Creates a new C-compatible string from a container of bytes.
This method will consume the provided data and use the underlying bytes
to construct a new string, ensuring that there is a trailing 0 byte.
This function will return an error if the bytes yielded contain an
internal 0 byte. The error returned will contain the bytes as well as
the position of the nul byte.
Creates a C-compatible string from a byte vector without checking for
interior 0 bytes.
This method is equivalent to new
except that no runtime assertion
is made that v
contains no 0 bytes, and it requires an actual
byte vector, not anything that can be converted to one with Into.
Retakes ownership of a CString
that was transferred to C.
Additionally, the length of the string will be recalculated from the pointer.
This should only ever be called with a pointer that was earlier
obtained by calling into_raw
on a CString
. Other usage (e.g. trying to take
ownership of a string that was allocated by foreign code) is likely to lead
to undefined behavior or allocator corruption.
Transfers ownership of the string to a C caller.
The pointer must be returned to Rust and reconstituted using
from_raw
to be properly deallocated. Specifically, one
should not use the standard C free
function to deallocate
this string.
Failure to call from_raw
will lead to a memory leak.
Converts the CString
into a String
if it contains valid Unicode data.
On failure, ownership of the original CString
is returned.
Returns the underlying byte buffer.
The returned buffer does not contain the trailing nul separator and
it is guaranteed to not have any interior nul bytes.
Equivalent to the into_bytes
function except that the returned vector
includes the trailing nul byte.
Returns the contents of this CString
as a slice of bytes.
The returned slice does not contain the trailing nul separator and
it is guaranteed to not have any interior nul bytes.
Equivalent to the as_bytes
function except that the returned slice
includes the trailing nul byte.
Extracts a CStr
slice containing the entire string.
Converts this CString
into a boxed CStr
.
Returns the inner pointer to this C string.
The returned pointer will be valid for as long as self
is, and points
to a contiguous region of memory terminated with a 0 byte to represent
the end of the string.
Converts this C string to a byte slice.
The returned slice will not contain the trailing nul terminator that this C
string has.
Note: This method is currently implemented as a constant-time
cast, but it is planned to alter its definition in the future to
perform the length calculation whenever this method is called.
Converts this C string to a byte slice containing the trailing 0 byte.
This function is the equivalent of to_bytes
except that it will retain
the trailing nul terminator instead of chopping it off.
Note: This method is currently implemented as a 0-cost cast, but
it is planned to alter its definition in the future to perform the
length calculation whenever this method is called.
Yields a &str
slice if the CStr
contains valid UTF-8.
If the contents of the CStr
are valid UTF-8 data, this
function will return the corresponding &str
slice. Otherwise,
it will return an error with details of where UTF-8 validation failed.
Note: This method is currently implemented to check for validity
after a constant-time cast, but it is planned to alter its definition
in the future to perform the length calculation in addition to the
UTF-8 check whenever this method is called.
Converts a CStr
into a Cow
<
str
>
.
If the contents of the CStr
are valid UTF-8 data, this
function will return a Cow
::
Borrowed
(
[&str
])
with the the corresponding [&str
] slice. Otherwise, it will
replace any invalid UTF-8 sequences with U+FFFD REPLACEMENT CHARACTER
and return a Cow
::
[Owned
](
String
)
with the result.
Note: This method is currently implemented to check for validity
after a constant-time cast, but it is planned to alter its definition
in the future to perform the length calculation in addition to the
UTF-8 check whenever this method is called.
The resulting type after dereferencing.
Formats the value using the given formatter. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the minimum of two values. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
Executes the destructor for this type. Read more
The returned type after indexing.
Performs the indexing (container[index]
) operation.
Performs copy-assignment from source
. Read more
Creates an empty CString
.
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more