将 `usize` 转换为 `&str` 的最佳方法是什么? [英] What is the optimal way to convert a `usize` to a `&str`?

查看:67
本文介绍了将 `usize` 转换为 `&str` 的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 usize 转换为 &str 以传递给 fn foo(&str).我找到了以下两种方式,但不知道使用as_str()Deref 有没有区别.也许 selfas_str 中完成的工作以某种方式与 Deref 链接?我不知道使用一种或另一种是否更好,或者它们实际上是否相同.

  1. 使用temp.to_string().as_str():

    #[内联]#[stable(feature = "string_as_str", 因为 = "1.7.0")]pub fn as_str(&self) ->&str {自己}

  2. 使用 &*temp.to_string()&temp.to_string().这通过 Deref 起作用:

    #[stable(feature = "rust1", since = "1.0.0")]impl ops::Deref for String {类型目标 = str;#[排队]fn deref(&self) ->&str {不安全 { str::from_utf8_unchecked(&self.vec) }}}

<块引用>

问题可能取决于你想在 foo 中做什么:&str 是否需要比 foo 存活时间更长?

foo(&str)s: &str 在这段代码中的最小示例:

extern crate termbox_sys 作为 termbox;pub fn print(&self, x: usize, y: usize, sty: Style, fg: Color, bg: Color, s: &str) {让 fg = Style::from_color(fg) |(麦粒肿和样式::TB_ATTRIB);让 bg = Style::from_color(bg);for (i, ch) in s.chars().enumerate() {不安全{self.change_cell(x + i, y, ch as u32, fg.bits(), bg.bits());}}}pub unsafe fn change_cell(&self, x: usize, y: usize, ch: u32, fg: u16, bg: u16) {termbox::tb_change_cell(x 作为 c_int, y 作为 c_int, ch, fg, bg)}

termbox_sys

解决方案

如您所见,as_str 似乎没有做任何事情.事实上,它返回self,一个&String,其中需要一个&str.这会导致编译器插入对 Deref 的调用.所以你的两种方式是完全一样的.

I need to convert a usize to a &str to pass to a fn foo(&str). I found the following two ways, but don't know if there is a difference between using as_str() or Deref. Maybe the work done by self in as_str links with Deref somehow? I do not know if it may be better to use one or the other or if they actually are the same.

  1. Using temp.to_string().as_str():

    #[inline]
    #[stable(feature = "string_as_str", since = "1.7.0")]
    pub fn as_str(&self) -> &str {
        self
    }
    

  2. Using &*temp.to_string() or &temp.to_string(). This works via Deref:

    #[stable(feature = "rust1", since = "1.0.0")]
    impl ops::Deref for String {
        type Target = str;
    
        #[inline]
        fn deref(&self) -> &str {
            unsafe { str::from_utf8_unchecked(&self.vec) }
        }
    }
    

The question may depend on what you want to do in foo: does the passed &str need to outlive foo or not ?

foo(&str) is a minimal example for s: &str in this code:

extern crate termbox_sys as termbox;

pub fn print(&self, x: usize, y: usize, sty: Style, fg: Color, bg: Color, s: &str) {
    let fg = Style::from_color(fg) | (sty & style::TB_ATTRIB);
    let bg = Style::from_color(bg);
    for (i, ch) in s.chars().enumerate() {
        unsafe {
            self.change_cell(x + i, y, ch as u32, fg.bits(), bg.bits());
        }
    }
}

pub unsafe fn change_cell(&self, x: usize, y: usize, ch: u32, fg: u16, bg: u16) {
    termbox::tb_change_cell(x as c_int, y as c_int, ch, fg, bg)
}

termbox_sys

解决方案

As you noticed, as_str doesn't appear to do anything. In fact it returns self, a &String, where a &str is expected. This causes the compiler to insert a call to Deref. So your two ways are exactly the same.

这篇关于将 `usize` 转换为 `&amp;str` 的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆