当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust String.replace_range用法及代码示例


本文简要介绍rust语言中 alloc::string::String.replace_range 的用法。

用法

pub fn replace_range<R>(&mut self, range: R, replace_with: &str) where    R: RangeBounds<usize>,

删除字符串中的指定范围,并将其替换为给定的字符串。给定的字符串不需要与范围的长度相同。

Panics

如果起点或终点不在 char 边界上,或者它们超出边界,则会出现Panics。

例子

基本用法:

let mut s = String::from("α is alpha, β is beta");
let beta_offset = s.find('β').unwrap_or(s.len());

// Replace the range up until the β from the string
s.replace_range(..beta_offset, "Α is capital alpha; ");
assert_eq!(s, "Α is capital alpha; β is beta");

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 alloc::string::String.replace_range。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。