【发布时间】: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();
}
}
【问题讨论】:
-
<String>应该在这里是什么? -
让 mut line = String::new();这是从 line = read!(); 填充的并传递给函数 process_line。
-
我说的是函数签名。
<String>本身不是有效类型,仅作为::AssociatedType 的一部分。 -
啊,我假设我实际上想要 &str 因为这个函数只需要读取字符串。如何知道一个类型是否需要
::AssocuatedType 模式? -
问题是
<String>vsString。<>包装器没有意义。
标签: rust