相关疑难解决方法(0)

将本地String作为切片返回(&str)

有几个问题似乎与我遇到的问题有关.例如,请看这里这里.基本上我正在尝试String在本地函数中构建一个函数,但是然后将其作为一个函数返回&str.切片不起作用,因为寿命太短.我无法str直接在函数中使用,因为我需要动态构建它.但是,我也不想返回a,String因为一旦它构建完成,它进入的对象的性质就是静态的.有没有办法让我的蛋糕也吃?

这是一个最小的非编译复制:

fn return_str<'a>() -> &'a str {
    let mut string = "".to_string();

    for i in 0..10 {
        string.push_str("ACTG");
    }

    &string[..]
}
Run Code Online (Sandbox Code Playgroud)

rust

37
推荐指数
3
解决办法
8961
查看次数

索引操作的返回类型是什么?

我正在努力,非常不成功地玩切片.

我把我的第一个问题减少到:

fn at<'a, T>(slice: &'a [T], index: usize) -> &'a T {
    let item = slice[index];
    item
}
Run Code Online (Sandbox Code Playgroud)

slice[index]根据文档,我希望返回类型是一个参考:

pub trait Index<Index> {
    type Output;
    fn index(&'a self, index: &Index) -> &'a <Self as Index<Index>>::Output;
//                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
Run Code Online (Sandbox Code Playgroud)

但是,编译器给我一个错误:

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
3 |     item
  |     ^^^^ expected reference, found type parameter
  |
  = note: expected type `&'a T`
             found type `T`
Run Code Online (Sandbox Code Playgroud)

我将其解释为意味着item类型与函数的返回类型不匹配(我item仅为调试目的引入,从返回中拆分表达式求值).

如果我将返回类型切换为T,这是类型item,我得到另一个错误消息:

error[E0508]: …
Run Code Online (Sandbox Code Playgroud)

rust

11
推荐指数
2
解决办法
961
查看次数

为什么 str 主要以借用形式存在?

这是该str类型的使用方式:

let hello = "Hello, world!";

// with an explicit type annotation
let hello: &'static str = "Hello, world!";
Run Code Online (Sandbox Code Playgroud)

let hello: str = "Hello, world!"; 造成 expected `str`, found `&str`

为什么文本的默认类型str与所有原始类型、向量和String? 为什么是参考?

string types reference rust borrowing

6
推荐指数
1
解决办法
554
查看次数

如何编写一个返回Vec <Path>的函数?

我正在阅读文档并尝试编写一些基本文件I/O代码作为工具来帮助我学习Rust.

以下内容无法编译:

use std::fs;
use std::io;
use std::path::Path;

pub fn read_filenames_from_dir<P>(path: P) -> Result<Vec<Path>, io::Error>
where
    P: AsRef<Path>,
{
    let paths = try!(fs::read_dir(path));
    Ok(paths.unwrap())
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

error[E0277]: the trait bound `[u8]: std::marker::Sized` is not satisfied in `std::path::Path`
  --> src/main.rs:5:1
   |
5  | / pub fn read_filenames_from_dir<P>(path: P) -> Result<Vec<Path>, io::Error>
6  | | where
7  | |     P: AsRef<Path>,
8  | | {
9  | |     let paths = try!(fs::read_dir(path));
10 | |     Ok(paths.unwrap())
11 | | }
   | |_^ …
Run Code Online (Sandbox Code Playgroud)

rust

5
推荐指数
1
解决办法
983
查看次数

如何从函数返回固定大小的字符串文字数组?

此函数适用于 type i32,但适用于 type str

fn getValues() -> [str; 2] {
    [
        "37107287533902102798797998220837590246510135740250",
        "46376937677490009712648124896970078050417018260538",
    ]
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

fn getValues() -> [str; 2] {
    [
        "37107287533902102798797998220837590246510135740250",
        "46376937677490009712648124896970078050417018260538",
    ]
}
Run Code Online (Sandbox Code Playgroud)

这个错误让我觉得我需要添加大小,但我做到了:大小为 2。 Rust 想要什么?

arrays string rust

2
推荐指数
1
解决办法
3071
查看次数

标签 统计

rust ×5

string ×2

arrays ×1

borrowing ×1

reference ×1

types ×1