Struct std::string::FromUtf8Error1.0.0[][src]

pub struct FromUtf8Error { /* fields omitted */ }
Expand description

从 UTF-8 字节 vector 转换 String 时可能的错误值。

该类型是 Stringfrom_utf8 方法的错误类型。 它的设计方式旨在避免重新分配: into_bytes 方法将返回转换尝试中使用的字节 vector。

std::str 提供的 Utf8Error 类型表示将 u8 的切片转换为 &str 时可能发生的错误。 从这个意义上讲,它是 FromUtf8Error 的类似物,您可以通过 utf8_error 方法从 FromUtf8Error 中获得一个。

Examples

基本用法:

// vector 中的一些无效字节
let bytes = vec![0, 159];

let value = String::from_utf8(bytes);

assert!(value.is_err());
assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
Run

Implementations

返回试图转换为 Stringu8 个字节的片段。

Examples

基本用法:

// vector 中的一些无效字节
let bytes = vec![0, 159];

let value = String::from_utf8(bytes);

assert_eq!(&[0, 159], value.unwrap_err().as_bytes());
Run

返回尝试转换为 String 的字节。

精心构造此方法以避免分配。 它将消耗错误,将字节移出,因此不需要制作字节的副本。

Examples

基本用法:

// vector 中的一些无效字节
let bytes = vec![0, 159];

let value = String::from_utf8(bytes);

assert_eq!(vec![0, 159], value.unwrap_err().into_bytes());
Run

提取 Utf8Error 以获取有关转换失败的更多详细信息。

std::str 提供的 Utf8Error 类型表示将 u8 的切片转换为 &str 时可能发生的错误。 从这个意义上讲,它类似于 FromUtf8Error。 有关使用它的更多详细信息,请参见其文档。

Examples

基本用法:

// vector 中的一些无效字节
let bytes = vec![0, 159];

let error = String::from_utf8(bytes).unwrap_err().utf8_error();

// 第一个字节在这里无效
assert_eq!(1, error.valid_up_to());
Run

Trait Implementations

返回值的副本。 Read more

source 执行复制分配。 Read more

使用给定的格式化程序格式化该值。 Read more

使用给定的格式化程序格式化该值。 Read more

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

此错误的下级来源 (如果有)。 Read more

🔬 This is a nightly-only experimental API. (backtrace #53487)

返回发生错误的栈回溯 (如果有)。 Read more

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

此方法测试 selfother 值是否相等,并由 == 使用。 Read more

此方法测试 !=

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

获得所有权后的结果类型。

通常通过克隆从借用数据中创建拥有的数据。 Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

recently added

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more

将给定值转换为 StringRead more

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。