使用#![no_std]时如何访问StrExt等核心特征?

rag*_*oth 0 rust

我正在尝试用#![no_std]set 写一些Rust .我试图一次迭代str一个字符; 然而,通常的技术似乎都不起作用.当我尝试通过str提供的函数访问字符时,例如,for c in s.char_indices()或者for c in s.chars()我收到错误:

type &str does not implement any method in scope named ___
Run Code Online (Sandbox Code Playgroud)

我的理解是str是其中的一部分,core因此它所实现的任何特性都应该可用no_std.有没有办法访问此功能,或另一种迭代方式str

huo*_*uon 5

您需要导入特征才能调用他们的方法,例如

#![no_std]
extern crate core;

use core::str::StrExt;

fn foo(s: &str) {
    for c in s.char_indices() {}
}
Run Code Online (Sandbox Code Playgroud)

core还提供了另一种前奏,其中包括std::prelude可用的功能core.你可以导入它use core::prelude::*;.