Struct std::ffi::OsString1.0.0[][src]

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

一种类型,可以表示拥有的,可变的平台原生字符串,但可以廉价地与 Rust 字符串互转换。

对这种类型的需求源于以下事实:

  • 在 Unix 系统上,字符串通常是非零字节的任意序列,在许多情况下解释为 UTF-8。

  • 在 Windows 上,字符串通常是非零的 16 位值的任意序列,如果有效,则将其解释为 UTF-16。

  • 在 Rust 中,字符串始终是有效的 UTF-8,其中可能包含零。

OsStringOsStr 通过同时表示 Rust 和平台原生字符串值来弥合这一差距,特别是允许 Rust 字符串在可能的情况下免费转换为 “OS” 字符串。 这样的结果是 OsString 实例不是 * NUL 终止的; 为了传递给例如 Unix 系统调用,您应该创建一个 CStr

OsString&OsStr 如同 String&str: 每对中的前一个都是拥有所有权的字符串; 后者是借用的。

注意,OsStringOsStr 在内部不一定要以平台固有的形式保存字符串。在 Unix 上,字符串被存储为 8 位值的序列,在 Windows 上,字符串是基于 16 位值的,正如刚才所讨论的,字符串实际上也被存储为 8 位值的序列,用一种不太严格的 UTF-8 变体编码。

这对于理解处理容量和长度值时很有用。

创建一个 OsString

来自 Rust 字符串: OsString 实现了 From<String>,因此您可以使用 my_string.into() 从普通 Rust 字符串创建 OsString

**从切片: **就像您可以从一个空的 Rust String 开始,然后用 String::push_str 将一些 &str 子字符串切片放入其中一样,您也可以使用 OsString::new 方法创建一个空的 OsString,然后使用 OsString::push 方法将字符串切片推入其中。

提取整个操作系统字符串中的借用引用

您可以使用 OsString::as_os_str 方法从 OsString 获取 &OsStr ; 这实际上是对整个字符串的借用引用。

Conversions

请参见模块中关于 转换 的顶级文档,以讨论为 OsString from/to 原生表示形式的 转换 而实现的特征。

Implementations

创建一个新的空 OsString

Examples
use std::ffi::OsString;

let os_string = OsString::new();
Run

转换为 OsStr 切片。

Examples
use std::ffi::{OsString, OsStr};

let os_string = OsString::from("foo");
let os_str = OsStr::new("foo");
assert_eq!(os_string.as_os_str(), os_str);
Run

如果 OsString 包含有效的 Unicode 数据,则将其转换为 String

失败时,将返回原始 OsString 的所有权。

Examples
use std::ffi::OsString;

let os_string = OsString::from("foo");
let string = os_string.into_string();
assert_eq!(string, Ok(String::from("foo")));
Run

用给定的 &OsStr 切片扩展字符串。

Examples
use std::ffi::OsString;

let mut os_string = OsString::from("foo");
os_string.push("bar");
assert_eq!(&os_string, "foobar");
Run

创建具有给定容量的新 OsString

该字符串将能够完全容纳其他 OS 字符串的 capacity 长度单位,而无需重新分配。 如果 capacity 为 0,则不会分配该字符串。

请参见有关编码的主要 OsString 文档信息。

Examples
use std::ffi::OsString;

let mut os_string = OsString::with_capacity(10);
let capacity = os_string.capacity();

// 无需重新分配即可完成此推送
os_string.push("foo");

assert_eq!(capacity, os_string.capacity());
Run

OsString 截断为零长度。

Examples
use std::ffi::OsString;

let mut os_string = OsString::from("foo");
assert_eq!(&os_string, "foo");

os_string.clear();
assert_eq!(&os_string, "");
Run

返回此 OsString 无需重新分配即可容纳的容量。

有关编码的信息,请参见 OsString 简介。

Examples
use std::ffi::OsString;

let os_string = OsString::with_capacity(10);
assert!(os_string.capacity() >= 10);
Run

为给定的 OsString 插入至少至少 additional 的容量保留容量。

该集合可以保留更多空间,以避免频繁的重新分配。

Examples
use std::ffi::OsString;

let mut s = OsString::new();
s.reserve(10);
assert!(s.capacity() >= 10);
Run
🔬 This is a nightly-only experimental API. (try_reserve_2 #91789)

尝试在给定的 OsString 中为至少 additional 多个长度单位保留容量。 字符串可能会保留更多空间,以避免频繁的重新分配。 调用 try_reserve 后,容量将大于或等于 self.len() + additional

如果容量已经足够,则不执行任何操作。

Errors

如果容量溢出,或者分配器报告失败,则返回错误。

Examples
#![feature(try_reserve_2)]
use std::ffi::{OsStr, OsString};
use std::collections::TryReserveError;

fn process_data(data: &str) -> Result<OsString, TryReserveError> {
    let mut s = OsString::new();

    // 预先保留内存,如果不能,则退出
    s.try_reserve(OsStr::new(data).len())?;

    // 现在我们知道在我们复杂的工作中这不能 OOM
    s.push(data);

    Ok(s)
}
Run

保留最小容量,以便将 additional 精确地插入给定的 OsString 中。

如果容量已经足够,则不执行任何操作。

请注意,分配器可能会给集合提供比其请求更多的空间。 因此,不能依靠容量来精确地最小化。 如果预计将来会插入,则最好使用 reserve

Examples
use std::ffi::OsString;

let mut s = OsString::new();
s.reserve_exact(10);
assert!(s.capacity() >= 10);
Run
🔬 This is a nightly-only experimental API. (try_reserve_2 #91789)

尝试在给定的 OsString 中为精确的 additional 多个长度单位保留最小容量。 调用 try_reserve_exact 后,如果返回 Ok(()),则容量将大于或等于 self.len() + additional

如果容量已经足够,则不执行任何操作。

请注意,分配器可能会为 OsString 提供比它请求更多的空间。 因此,不能依靠容量来精确地最小化。 如果希望将来插入,则首选 try_reserve

Errors

如果容量溢出,或者分配器报告失败,则返回错误。

Examples
#![feature(try_reserve_2)]
use std::ffi::{OsStr, OsString};
use std::collections::TryReserveError;

fn process_data(data: &str) -> Result<OsString, TryReserveError> {
    let mut s = OsString::new();

    // 预先保留内存,如果不能,则退出
    s.try_reserve_exact(OsStr::new(data).len())?;

    // 现在我们知道在我们复杂的工作中这不能 OOM
    s.push(data);

    Ok(s)
}
Run

缩小 OsString 的容量以使其长度匹配。

Examples
use std::ffi::OsString;

let mut s = OsString::from("foo");

s.reserve(100);
assert!(s.capacity() >= 100);

s.shrink_to_fit();
assert_eq!(3, s.capacity());
Run

降低 OsString 的容量。

容量将至少保持与长度和提供的值一样大。

如果当前容量小于下限,则为无操作。

Examples
use std::ffi::OsString;

let mut s = OsString::from("foo");

s.reserve(100);
assert!(s.capacity() >= 100);

s.shrink_to(10);
assert!(s.capacity() >= 10);
s.shrink_to(0);
assert!(s.capacity() >= 3);
Run

将此 OsString 转换为 boxed OsStr

Examples
use std::ffi::{OsString, OsStr};

let s = OsString::from("hello");

let b: Box<OsStr> = s.into_boxed_os_str();
Run

Methods from Deref<Target = OsStr>

如果 OsStr 是有效的 Unicode,则产生 &str

此转换可能需要检查 UTF-8 有效性。

Examples
use std::ffi::OsStr;

let os_str = OsStr::new("foo");
assert_eq!(os_str.to_str(), Some("foo"));
Run

OsStr 转换为 Cow<str>

任何非 Unicode 序列都将替换为 U+FFFD REPLACEMENT CHARACTER

Examples

使用无效的 Unicode 在 OsStr 上调用 to_string_lossy

// 注意,由于 Unix 和 Windows 表示字符串的方式不同,我们不得不使该示例复杂化,使用不同的源数据和通过不同的平台扩展来设置示例 `OsStr`。
// 可以理解,实际上,仅通过收集用户命令行参数,您就可以得到这样的示例无效序列。

#[cfg(unix)] {
    use std::ffi::OsStr;
    use std::os::unix::ffi::OsStrExt;

    // 此处,值 0x66 和 0x6f 分别对应于 'f' 和 'o'。
    // 值 0x80 是一个单独的连续字节,在 UTF-8 序列中无效。
    let source = [0x66, 0x6f, 0x80, 0x6f];
    let os_str = OsStr::from_bytes(&source[..]);

    assert_eq!(os_str.to_string_lossy(), "fo�o");
}
#[cfg(windows)] {
    use std::ffi::OsString;
    use std::os::windows::prelude::*;

    // 在此,值 0x0066 和 0x006f 分别对应于 'f' 和 'o'。
    // 值 0xD800 是一个单独的替代一半,在 UTF-16 序列中无效。
    let source = [0x0066, 0x006f, 0xD800, 0x006f];
    let os_string = OsString::from_wide(&source[..]);
    let os_str = os_string.as_os_str();

    assert_eq!(os_str.to_string_lossy(), "fo�o");
}
Run

将切片复制到拥有的 OsString 中。

Examples
use std::ffi::{OsStr, OsString};

let os_str = OsStr::new("foo");
let os_string = os_str.to_os_string();
assert_eq!(os_string, OsString::from("foo"));
Run

检查 OsStr 是否为空。

Examples
use std::ffi::OsStr;

let os_str = OsStr::new("");
assert!(os_str.is_empty());

let os_str = OsStr::new("foo");
assert!(!os_str.is_empty());
Run

返回此 OsStr 的长度。

请注意,这不会以 OS 字符串形式返回字符串中的字节数。

返回的长度是 OsStr 使用的底层存储的长度。 如 OsString 简介中所讨论的,OsStringOsStr 以最适合于原生平台和 Rust 字符串形式之间的廉价相互转换的形式存储字符串,这两种形式在存储大小和编码方面可能都大不相同。

此数字对于传递给其他方法 (例如 OsString::with_capacity) 以避免重新分配非常有用。

Examples
use std::ffi::OsStr;

let os_str = OsStr::new("");
assert_eq!(os_str.len(), 0);

let os_str = OsStr::new("foo");
assert_eq!(os_str.len(), 3);
Run

将此字符串就地转换为其 ASCII 小写等效项。

ASCII 字母 ‘A’ 到 ‘Z’ 映射到 ‘a’ 到 ‘z’,但是非 ASCII 字母不变。

要返回新的小写值而不修改现有值,请使用 OsStr::to_ascii_lowercase

Examples
use std::ffi::OsString;

let mut s = OsString::from("GRÜßE, JÜRGEN ❤");

s.make_ascii_lowercase();

assert_eq!("grÜße, jÜrgen ❤", s);
Run

将此字符串就地转换为其 ASCII 大写等效项。

ASCII 字母 ‘a’ 到 ‘z’ 映射到 ‘A’ 到 ‘Z’,但是非 ASCII 字母不变。

要返回新的大写值而不修改现有值,请使用 OsStr::to_ascii_uppercase

Examples
use std::ffi::OsString;

let mut s = OsString::from("Grüße, Jürgen ❤");

s.make_ascii_uppercase();

assert_eq!("GRüßE, JüRGEN ❤", s);
Run

返回此字符串的副本,其中每个字符都映射为其等效的 ASCII 小写字母。

ASCII 字母 ‘A’ 到 ‘Z’ 映射到 ‘a’ 到 ‘z’,但是非 ASCII 字母不变。

要就地小写该值,请使用 OsStr::make_ascii_lowercase

Examples
use std::ffi::OsString;
let s = OsString::from("Grüße, Jürgen ❤");

assert_eq!("grüße, jürgen ❤", s.to_ascii_lowercase());
Run

返回此字符串的副本,其中每个字符都映射为其等效的 ASCII 大写字母。

ASCII 字母 ‘a’ 到 ‘z’ 映射到 ‘A’ 到 ‘Z’,但是非 ASCII 字母不变。

要就地将值大写,请使用 OsStr::make_ascii_uppercase

Examples
use std::ffi::OsString;
let s = OsString::from("Grüße, Jürgen ❤");

assert_eq!("GRüßE, JüRGEN ❤", s.to_ascii_uppercase());
Run

检查此字符串中的所有字符是否都在 ASCII 范围内。

Examples
use std::ffi::OsString;

let ascii = OsString::from("hello!\n");
let non_ascii = OsString::from("Grüße, Jürgen ❤");

assert!(ascii.is_ascii());
assert!(!non_ascii.is_ascii());
Run

检查两个字符串是否为 ASCII 不区分大小写的匹配项。

to_ascii_lowercase(a) == to_ascii_lowercase(b) 相同,但不分配和复制临时文件。

Examples
use std::ffi::OsString;

assert!(OsString::from("Ferris").eq_ignore_ascii_case("FERRIS"));
assert!(OsString::from("Ferrös").eq_ignore_ascii_case("FERRöS"));
assert!(!OsString::from("Ferrös").eq_ignore_ascii_case("FERRÖS"));
Run

Trait Implementations

执行转换。

执行转换。

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

返回值的副本。 Read more

source 执行复制分配。 Read more

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

创建一个空的 OsString

解引用后的结果类型。

解引用值。

可变地解引用该值。

使用迭代器的内容扩展集合。 Read more

🔬 This is a nightly-only experimental API. (extend_one #72631)

用一个元素扩展一个集合。

🔬 This is a nightly-only experimental API. (extend_one #72631)

在集合中为给定数量的附加元素保留容量。 Read more

使用迭代器的内容扩展集合。 Read more

🔬 This is a nightly-only experimental API. (extend_one #72631)

用一个元素扩展一个集合。

🔬 This is a nightly-only experimental API. (extend_one #72631)

在集合中为给定数量的附加元素保留容量。 Read more

使用迭代器的内容扩展集合。 Read more

🔬 This is a nightly-only experimental API. (extend_one #72631)

用一个元素扩展一个集合。

🔬 This is a nightly-only experimental API. (extend_one #72631)

在集合中为给定数量的附加元素保留容量。 Read more

执行转换。

执行转换。

Box<OsStr> 转换为 OsString,而无需复制或分配。

执行转换。

OsString 转换为 Box<OsStr>,而无需复制或分配。

OsString 转换为 Arc<OsStr>,而无需复制或分配。

OsString 转换为 Rc<OsStr>,而无需复制或分配。

执行转换。

OsString 转换为 PathBuf

此转换不会分配或复制内存。

PathBuf 转换为 OsString

此转换不会分配或复制内存。

String 转换为 OsString

此转换不会分配或复制内存。

从迭代器创建一个值。 Read more

从迭代器创建一个值。 Read more

从迭代器创建一个值。 Read more

可以从解析中返回的相关错误。

解析字符串 s 以返回此类型的值。 Read more

将该值输入给定的 HasherRead more

将这种类型的切片送入给定的 Hasher 中。 Read more

索引后返回的类型。

执行索引 (container[index]) 操作。 Read more

执行可变索引 (container[index]) 操作。 Read more

此方法返回 selfother 之间的 OrderingRead more

比较并返回两个值中的最大值。 Read more

比较并返回两个值中的最小值。 Read more

将值限制在某个时间间隔内。 Read more

从字节 vector 创建 OsStringRead more

产生此 OsString 的底层字节 vector。 Read more

从字节 vector 创建 OsStringRead more

产生此 OsString 的底层字节 vector。 Read more

从可能是格式不正确的 UTF-16 切片创建 OsString 16 位代码单元。 Read more

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

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

此方法测试 !=

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 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)

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

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

执行转换。

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

执行转换。