【问题标题】:Rust Function Signature resulting in: error: expected `::`, found `,`Rust 函数签名导致:错误:预期的 `::`, found `,`
【发布时间】:2021-04-16 21:55:00
【问题描述】:

上下文

我正在用 rust 编写一个 mini-shell 来学习这门语言。在这一点上,我实际上是该语言的新手,所以我认为这是一个相当基本的误解。

小问题

我正在写一个函数签名 在哪里

fn process_line(line: <String>, built_ins: HashMap<&str, BuiltinFn>) -> i8 {

fn process_line(line: <String>) -> i8

导致下两节显示以下错误,但此签名

fn process_line() -> i8 {

成功了,我不知道为什么。我已经尝试查找 rust 中的 :: 运算符是什么,并搜索此特定消息以获取 rust 函数签名,但我并不真正理解这里的问题。我能猜到的最好的结果是编译器希望我为参数定义一组特征?

两个参数错误消息

cargo test
   Compiling rust_bash v0.1.0 (/Users/Alex.LordThorsen/git/misc/rust_bash)
error: expected `::`, found `,`
  --> src/main.rs:26:31
   |
26 | fn process_line(line: <String>, built_ins: HashMap<&str, BuiltinFn>) -> i8 {
   |                               ^ expected `::`

error: aborting due to previous error

error: could not compile `rust_bash`

To learn more, run the command again with --verbose.

一个参数错误消息

cargo test
   Compiling rust_bash v0.1.0 (/Users/Alex.LordThorsen/git/misc/rust_bash)
error: expected `::`, found `)`
  --> src/main.rs:27:31
   |
27 | fn process_line(line: <String>) -> i8 {
   |                               ^ expected `::`

warning: unused import: `std::collections::HashMap`
  --> src/main.rs:15:5
   |
15 | use std::collections::HashMap;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

warning: unused import: `BuiltinFn`
  --> src/main.rs:22:32
   |
22 | use builtins::{load_built_ins, BuiltinFn};
   |                                ^^^^^^^^^

error: aborting due to previous error; 2 warnings emitted

error: could not compile `rust_bash`

To learn more, run the command again with --verbose.

项目结构

~/git/misc/rust_bash/src :)% tree
.
├── README.md
├── argument_parsing.rs
├── builtins
│   ├── cd.rs
│   ├── echo.rs
│   └── mod.rs
└── main.rs

main.rs

mod argument_parsing;
mod builtins;

use std::env;
use std::collections::HashMap;
use log;

use simple_logger::SimpleLogger;
use text_io::read;

use argument_parsing::parse_arguments;
use builtins::{load_built_ins, BuiltinFn};



//fn process_line(line: <String>, built_ins: HashMap<&str, BuiltinFn>) -> i8 {
fn process_line(line: <String>) -> i8 {
    // If the requested argument is a builtin don't fork and directly run the
    // requested action.

    // If the function is not a builtin then first fork, have the child run the
    // command while the parent waits, and set the current exit status to the
    // commands exit status.
    return 0
}

fn process_io() {
    let mut line = String::new();
    let mut status_code: i8;
    // Loading Built ins here so that it's loaded once and passing to
    // process_line.
    let built_ins = load_built_ins();
    loop {
        // TODO: Add a prompt here.
        line = read!();
        log::debug!("Line to Process: {}", line);
        if line == "exit" {
            break;
        }
        status_code = process_line(line, built_ins)
    }
}

fn main() {
    SimpleLogger::from_env().init().unwrap();
    // env::args().collect() will automatically do quote groupings for us.
    // Technically this might mean we read the args string twice? Might want to
    // look into that.
    let args: Vec<String> = env::args().collect();
    let processed_args: Vec<String>;

    log::debug!("Args Before Processing: {:?}", args);
    processed_args = parse_arguments(args);
    log::debug!("Processed Args: {:?}", processed_args);

    if processed_args.len() == 0 {
        process_io();
    }
}

【问题讨论】:

  • &lt;String&gt; 应该在这里是什么?
  • 让 mut line = String::new();这是从 line = read!(); 填充的并传递给函数 process_line。
  • 我说的是函数签名。 &lt;String&gt; 本身不是有效类型,仅作为 ::AssociatedType 的一部分。
  • 啊,我假设我实际上想要 &str 因为这个函数只需要读取字符串。如何知道一个类型是否需要 ::AssocuatedType 模式?
  • 问题是&lt;String&gt; vs String&lt;&gt; 包装器没有意义。

标签: rust


【解决方案1】:

&lt;String&gt; 不是类型的有效语法。而不是

fn process_line(line: <String>) -> i8

你需要:

fn process_line(line: String) -> i8

尖括号永远不会在类型周围使用本身。尖括号用于参数到另一种类型,如您的其他用法HashMap&lt;&amp;str, BuiltinFn&gt;其中@987654325 @ 是泛型类型,&amp;str, BuiltinFn 是两个参数——这是正确的语法,但不是。


在某些情况下,尖括号还用于引用关联项(函数、类型或在 impl 块内声明的常量),例如关联类型 &lt;T as Iterator&gt;::Item 或关联函数 &lt;[i32]&gt;::get,但仅此而已只有当您看到&lt; 完全是之前 一个类型名称而不是之后 一个。 这就是为什么解析器告诉你“预期的::”:鉴于在解析类型时它首先看到&lt;,然后它想要一个&gt;::

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2017-01-17
    • 1970-01-01
    相关资源
    最近更新 更多