我正在嘗試為我目前正在撰寫的語言撰寫單元測驗,所以我正在努力在決議器中存盤和檢索變數,除非我收到錯誤
error[E0277]: the size for values of type `dyn std::fmt::Display` cannot be known at compilation time
--> parser/src/env.rs:48:33
|
48 | assert_eq!(String::from(format!("{}", *get_binding("test", &env).unwrap().get())), "hello".to_string())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-
time
|
= help: the trait `Sized` is not implemented for `dyn std::fmt::Display`
note: required by a bound in `ArgumentV1::<'a>::new`
--> /home/muppi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:314:20
|
314 | pub fn new<'b, T>(x: &'b T, f: fn(&T, &mut Formatter<'_>) -> Result) -> ArgumentV1<'b> {
| ^ required by this bound in `ArgumentV1::<'a>::new`
= note: this error originates in the macro `$crate::__export::format_args` (in Nightly builds, run with -Z macro-backtrace for m
ore info)
For more information about this error, try `rustc --explain E0277`.
error: could not compile `parser` due to previous error
我的代碼
pub mod env {
use crate::vars::vars::*;
use std::{collections::HashMap, fmt::Display};
#[derive(Default)]
pub struct Environ {
pub bindings: HashMap<String, Var<Box<dyn Display 'static>>>,
}
#[allow(dead_code)]
pub(crate) fn store_var(name: &str, var: Var<Box<dyn Display 'static>>, env: &mut Environ) {
env.bindings.insert(String::from(name), var);
}
#[allow(dead_code)]
pub(crate) fn get_binding<'a>(name: &'a str, env: &'a Environ) -> Result<&'a Var<Box<dyn Display>>, String> {
if env.bindings.contains_key(&name.to_string()) {
let val = env.bindings.get(&name.to_string());
Ok(val.unwrap())
} else {
Err("Key does not exist".to_string())
}
}
}
#[cfg(test)]
mod tests {
use super::env::{Environ, store_var, *};
use crate::vars::vars::*;
#[test]
fn store_binding() {
let mut env = Environ::default();
store_var("test", Var::<i32>::new(Box::new(5), None), &mut env);
assert_eq!(env.bindings.len(), 1)
}
#[test]
#[should_panic(expected="value not found")]
fn retrieve_non_existent_binding() {
let mut env = Environ::default();
store_var("test", Var::<&str>::new(Box::new("hello"), None), &mut env);
if get_binding("this_does_exist", &env).is_err() {
panic!("value not found")
}
}
#[test]
fn retrieve_binding() {
let mut env = Environ::default();
store_var("test", Var::<&str>::new(Box::new("hello"), None), &mut env);
assert_eq!(String::from(format!("{}", *get_binding("test", &env).unwrap().get())), "hello".to_string())
}
}
最后一個測驗是發生錯誤的地方。這個測驗是為了使用格式!宏并格式化字串以生成一個包含存盤在變數中的資料的字串。變數庫
pub mod vars {
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub struct Var<T> {
pub val: Box<T>,
pub desc: Option<String>,
}
impl<T> std::ops::Deref for Var<T> {
type Target = Box<T>;
fn deref(&self) -> &Self::Target {
&self.val
}
}
impl<T> Var<T> {
pub fn new<Y>(val: Y, desc: Option<&str>) -> Var<Y> {
if let Some(desc) = desc {
Var {val: Box::new(val), desc: Some(desc.to_string())}
} else {
Var {val: Box::new(val), desc: None}
}
}
pub fn get(self) -> T {
*self.val
}
}
impl<T> fmt::Display for Var<T>
where
T: fmt::Display,
{
fn fmt(self: &Var<T>, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", *self.val)
}
}
}
#[cfg(test)]
mod tests {
use crate::vars::vars::*;
#[test]
fn new_var() {
assert_eq!(
Var::<i32>::new(10, Some("hello_world")),
Var {
val: Box::new(10),
desc: Some("hello_world".to_string())
}
)
}
}
uj5u.com熱心網友回復:
這個問題是不是在顯示幕上,那就是你不能說get()在&Var通過回傳get_binding()。例如,這不會編譯:
let x = get_binding("test", &env).unwrap().get();
這是因為get_binding()回傳到參考Var,而Var::get()取self按值(并且消耗它)。您通過添加*取消參考來解決問題的嘗試只會導致新的型別錯誤,該錯誤掩蓋了實際問題和解決問題的正確方法。
一旦根本原因顯而易見,顯而易見的解決方法是進行更改get(),使其接受&self并回傳一個參考,使其與由 回傳的參考兼容get_binding()。例如:
pub fn get(&self) -> &T {
&self.val
}
通過此更改,您的測驗將編譯并通過。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404648.html
標籤:
上一篇:如何跳過字串陣列中的第一個元素?
