我正在创建一个自定义Result,以使该Ok值是通用的,但我收到以下错误:
error[E0308]: mismatched types
--> src/main.rs:38:13
|
38 | Err(Error::new(ErrorKind::Other, "Not a subset"))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found enum `std::result::Result`
|
= note: expected type `()`
found type `std::result::Result<_, std::io::Error>`
Run Code Online (Sandbox Code Playgroud)
使用此代码:
use std::io;
use std::io::{Error, ErrorKind};
pub type SubsetResult<T> = Result<T, SubsetError>;
#[derive(Debug)]
pub enum SubsetError {
Io(io::Error),
}
impl From<io::Error> for SubsetError {
fn from(err: io::Error) -> SubsetError {
SubsetError::Io(err)
}
}
trait Subset {
type T;
fn is_subset(&self, &[i32]) -> SubsetResult<Self::T>;
}
#[derive(Debug)]
struct …Run Code Online (Sandbox Code Playgroud) 我试图在不同的静态方法中调用泛型静态方法,但我得到一个令人困惑的错误:
error: type annotations required: cannot resolve `_: Config` [--explain E0283]
--> src/main.rs:15:38
|>
15 |> "json" => return Config::parse_json::<T>(source, datatype),
|> ^^^^^^^^^^^^^^^^^^^^^^^
note: required by `Config::parse_json`
Run Code Online (Sandbox Code Playgroud)
我跑的时候rustc --explain E0283,错误信息说:
当编译器没有足够的信息来明确选择实现时,会发生此错误.
这是令人困惑的,因为只有一个函数的实现.
use rustc_serialize::json;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use rustc_serialize;
pub trait Config {
fn get_config<T: rustc_serialize::Decodable>(source: PathBuf, datatype: T) -> Option<T> {
let extension = source.extension().unwrap();
if let Some(extension) = extension.to_str() {
match extension {
"json" => return Config::parse_json::<T>(source, datatype),
_ => panic!("Unable to parse …Run Code Online (Sandbox Code Playgroud) 我正在尝试解析一个文件,该文件在每个值之间都有一个管道分隔符,每行都是一条新记录。我像这样迭代每一行:
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
fn main() {
let source_file = File::open("input.txt").unwrap();
let reader = BufReader::new(source_file);
for line in reader.lines() {
if let Some(channel_line) = line {
println!("yay");
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
fn main() {
let source_file = File::open("input.txt").unwrap();
let reader = BufReader::new(source_file);
for line in reader.lines() {
if let Some(channel_line) = line {
println!("yay");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个错误让我感到困惑,因为找到的类型是我所期望的,即Option<Result<String, Error>>如文档所示,Option因此假设我没有遗漏任何东西,在结果之前解开它是有意义的。