我正在研究一个基本的shell解释器,以熟悉Rust.在用于在shell中存储挂起作业的表上工作时,我遇到了以下编译器错误消息:
error: cannot invoke tuple struct constructor with private fields [E0450]
let jobs = job::JobsList(vec![]);
^~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我不清楚在这里看到什么是私人的.如下所示,两个结构都pub在我的模块文件中标记.那么,秘诀是什么?
mod job {
use std::fmt;
pub struct Job {
jid: isize,
pid: isize,
cmd: String,
}
pub struct JobsList(Vec<Job>);
}
fn main() {
let jobs = job::JobsList(vec![]);
}
Run Code Online (Sandbox Code Playgroud) 我可以解压这样的经典元组:
let pair = (1, true);
let (one, two) = pair;
Run Code Online (Sandbox Code Playgroud)
如果我有一个元组结构,如果我struct Matrix(f32, f32, f32, f32)尝试解压缩它,我得到一个关于"意外类型"的错误:
struct Matrix(f32, f32, f32, f32);
let mat = Matrix(1.1, 1.2, 2.1, 2.2);
let (one, two, three, four) = mat;
Run Code Online (Sandbox Code Playgroud)
结果出现此错误:
error[E0308]: mismatched types
--> src/main.rs:47:9
|
47 | let (one, two, three, four) = mat;
|
= note: expected type `Matrix`
found type `(_, _, _, _)`
Run Code Online (Sandbox Code Playgroud)
如何解压缩元组结构?我是否需要将其显式转换为元组类型?或者我需要硬编码吗?