Error: `Info` doesn't implement `Display` (required by {}) - rust

I got error: Info doesn't implement Display (required by {}):11 while running this code:
struct Info<'a> {
name: &'a str,
age: u8,
}
fn main() {
let john = Info {
name: "John",
age: 32,
};
println!("{}", john);
}
I have no idea what im doing wrong. Can anyone explain me?

In order for a struct to be formatable via "{}" format specifier it needs to implement the std::fmt::Display trait.
Therefore, to make your code compile, you need to impl Display for Info:
impl std::fmt::Display for Info<'_> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(fmt, "My name is {} and I'm {} years old.", self.name, self.age)
}
}
Note that Display trait is idiomatically used for user-facing representations.
Alternatively, you could use "{:?}" format specifier and #[derive(Debug)] annotation on your type to use formatter provided by std::fmt::Debug trait:
#[derive(Debug)]
struct Info<'a> {
name: &'a str,
age: u8,
}
fn main() {
let john = Info {
name: "John",
age: 32,
};
println!("{:?}", john);
}

Related

How to properly solve lifetime inferences in struct?

The Rust playground code is here.
I have a struct of Token which has lifetime 'tok, and a scanner has lifetime 'lexer. I'm using both of them in another struct Parser, then I had a problem:
pub struct Token<'tok> {
pub value: Cow<'tok, str>,
pub line: usize,
}
pub struct Scanner {
pub source: Vec<char>,
pub current: usize,
pub line: usize,
}
pub struct Parser<'lexer> {
pub curr: &'lexer Token<'lexer>,
pub prev: &'lexer Token<'lexer>,
scanner: &'lexer mut Scanner,
}
impl <'lexer> Parser<'lexer> {
pub fn advance(&mut self) {
self.prev = self.curr;
self.curr = &self.scanner.next(); // cannot inference lifetime
}
}
I think the problem is Token has lifetime 'tok, and the borrow checker doesn't know the relation between 'tok and 'lexer so it can't inference proper lifetime.
However, I can avoid the problem by modifying it into the updated code:
pub struct Parser<'lexer> {
pub curr: Token<'lexer>,
pub prev: Token<'lexer>,
scanner: &'lexer mut Scanner,
}
impl <'lexer> Parser<'lexer> {
pub fn advance(&mut self) {
let prev = std::mem::replace(&mut self.curr, self.scanner.next());
self.prev = prev;
}
}
And with Token produced by next() is static:
impl Scanner {
pub fn next(&mut self) -> Token<'static> {
Token {
value: Cow::from(""),
line: 0,
}
}
}
It does compile but I think it's not ideal because all tokens are cloned from scanner into parser(they are not references anymore) and living until end of Parser's life. So memory usage is doubled. Is there a more proper way to deal with this?
Actualy your code structure is not ideal to deal with the borrow checker here why:
Token struct should be owned, the struct itself do not require any allocations (as the token is owned some indrection are required to allow prev <-> next switching)
None of the Parser or Lexer should own the base data so it will be easy to bound proper lifetime
In our case Vec<Char> is not a friendly type to work with (we do not need to own the data and this will be harder to make the comipler understand lifetimes), instead we're going to use an &'str but you could reproduce the exact behaviours with an &[char])
Here is an example that compile just fine
pub struct Token<'source> {
pub value: Cow<'source, str>,
pub line: usize,
}
pub struct Scanner<'source> {
pub source: &'source str,
pub current: usize,
pub line: usize,
}
pub struct Parser<'source> {
pub curr: Option<Token<'source>>,
pub prev: Option<Token<'source>>,
scanner: Scanner<'source>,
}
impl <'source>Scanner<'source> {
pub fn next(&'source /* DONT Forget to bound 'source to `self` */ self) -> Token<'source> {
Token {
value: Cow::from(self.source), /* `self.source` is bound to `'source` so the compiler understand that the token lifetime is the same than the source's one */
line: 0,
}
}
}
impl <'lexer> Parser<'lexer> {
pub fn advance(&'lexer mut self) {
self.prev = self.curr.take();
self.curr = Some(self.scanner.next());
}
}

Error serializing struct: Expected struct JsValue, found Struct Error

I'm getting the error:
expected enum `std::result::Result<_, wasm_bindgen::JsValue>`
found enum `std::result::Result<_, serde_wasm_bindgen::error::Error>`
when I serialize a struct by implementing Serialize then passing it to serde_wasm_bindgen, which uses the example code from here
here is the min reproducable code:
use wasm_bindgen::prelude::*;
use serde::ser::{Serialize, SerializeStruct, Serializer};
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}
impl Serialize for Person {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut s = serializer.serialize_struct("Person", 3)?;
s.serialize_field("name", &self.name)?;
s.serialize_field("age", &self.age)?;
s.serialize_field("phones", &self.phones)?;
s.end()
}
}
#[wasm_bindgen]
pub fn pass_value_to_js() -> Result<JsValue, JsValue> {
let p = Person {
name: String::from("Hello"),
age: 56,
phones: vec![String::from("phone")],
};
serde_wasm_bindgen::to_value(p) // error here
}
Cargo.toml
serde-wasm-bindgen = "0.1.3"
serde = "1.0.114"
I followed this issue to solve the problem.
#[wasm_bindgen]
pub fn pass_value_to_js() -> Result<JsValue, JsValue> {
serde_wasm_bindgen::to_value(&value).map_err(|err| err.into())
}
I don't know anything about these packages, but looking at the code for to_value here it appears that the result is not compatible with Result<JsValue, JsValue>.
// The Error in scope here is serde_wasm_bindgen::error::Error
type Result<T> = std::result::Result<T, Error>;
...SNIP...
/// Converts a Rust value into a [`JsValue`].
pub fn to_value<T: serde::ser::Serialize>(value: &T) -> Result<JsValue> {
value.serialize(&Serializer::new())
}
So it looks like the function you have returning this value must change to be compatible with this new Result type.
I'm guessing you are referring to the example in the README here. I very often find that code examples in github READMEs are incorrect or out of date. Unfortunately I can't find an example or test in that repo that uses to_value() without immediately unwrap()ing it.

items from traits can only be used if the trait is implemented and in scope

I tried to implement the Observer pattern from Heads-up Design Patterns, which was originally written in Java:
use std::cell::RefCell;
use std::rc::Rc;
use std::borrow::BorrowMut;
trait Subject {
fn registerObserver(&mut self, observer: Rc<RefCell<Observer>>);
fn removeObserver(&mut self, observer: Rc<RefCell<Observer>>);
fn notifyObserver(&self, observer: Rc<RefCell<Observer>>);
}
trait Observer {
fn update(&mut self, data: f32);
}
struct Teacher {
observers: Vec<Rc<RefCell<Observer>>>,
data: f32,
}
impl Teacher {
pub fn print(&self) {
println!("teacher = {:}", self.data);
}
}
impl Subject for Teacher {
fn registerObserver(&mut self, observer: Rc<RefCell<Observer>>) {
self.observers.push(observer);
}
fn removeObserver(&mut self, observer: Rc<RefCell<Observer>>) {
println!("Teacher.removeObserver(...) not implemented yet...")
}
fn notifyObserver(&self, observer: Rc<RefCell<Observer>>) {
for observer in self.observers {
let mut loc_obs = observer.borrow_mut();
loc_obs.update(self.data);
}
}
}
struct Student {
data: f32,
}
impl Student {
pub fn print(&self) {
println!("student = {:}", self.data);
}
}
impl Observer for Student {
fn update(&mut self, data: f32) {
self.data = data;
}
}
fn main() {
let mut teacher = Teacher {
observers: Vec::new(),
data: 42.,
};
teacher.print();
}
Playground
The compiler tells me
error[E0599]: no method named `update` found for type `&mut std::rc::Rc<std::cell::RefCell<Observer + 'static>>` in the current scope
--> src/main.rs:35:21
|
35 | loc_obs.update(self.data);
| ^^^^^^
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `update`, perhaps you need to implement it:
candidate #1: `Observer`
Where is my error?
use std::borrow::BorrowMut;
You've brought in the trait BorrowMut, which defines the trait method BorrowMut::borrow_mut, shadowing the inherent method RefCell::borrow_mut. You can tell this because your type is not what you expect:
for type &mut std::rc::Rc<std::cell::RefCell<Observer + 'static>>
The simplest fix is to remove that import. You can also disambiguate between them.
You then have further issues around trying to take ownership of a borrowed value and lots of non-idiomatic names that create boatloads of warnings. You should address all of those.

Use trait as interface for database entity

I am trying to make an Entity interface for dynamically mapping a database result into a Rust struct:
pub trait Entity {
fn map(&self, Result<QueryResult>) -> Self;
}
pub struct DbQuery<T> {
pub sql: String,
pub params: Vec<Value>,
pub limit: i32,
pub paged: Option<Pagination>,
pub entity: T,
}
pub struct Settings {
pub name: String,
pub value: Option<String>,
}
impl Entity for Settings {
fn map(&self, result: Result<QueryResult>) -> Settings {
// ...
Settings {
name: "hello".to_string(),
value: None,
}
}
}
impl DbMapper {
// ...
pub fn find<T>(&self, query: DbQuery<T>) -> Option<Vec<T>> {
println!("query find SQL: {}", query.sql);
let mut stmt = &self.pool.prepare(query.sql).unwrap();
let ret = Vec::new();
for row in stmt.execute(query.params).unwrap() {
ret.push(query.entity.map(row.unwrap()));
}
Some(ret)
}
}
But I get an error:
error: no method named map found for type T in the current scope
ret.push(query.entity.map(row.unwrap())); |
note: the method map exists but the following trait
bounds were not satisfied: T : std::iter::Iterator = help: items
from traits can only be used if the trait is implemented and in scope;
the following traits define an item map, perhaps you need to
implement one of them: = help: candidate #1:
models::holders::database::Entity = help: candidate #2:
std::iter::Iterator
Here is a version of your code that runs on the playground and replicates your issue:
pub struct QueryResult;
pub struct Value;
pub struct Pagination;
pub struct DbMapper;
pub trait Entity {
fn map(&self, Result<QueryResult, ()>) -> Self;
}
pub struct DbQuery<T> {
pub sql: String,
pub params: Vec<Value>,
pub limit: i32,
pub paged: Option<Pagination>,
pub entity: T,
}
pub struct Settings {
pub name: String,
pub value: Option<String>,
}
impl Entity for Settings {
fn map(&self, result: Result<QueryResult, ()>) -> Settings {
// ...
Settings {
name: "hello".to_string(),
value: None,
}
}
}
impl DbMapper {
// ...
pub fn find<T>(&self, query: DbQuery<T>) -> Option<Vec<T>> {
println!("query find SQL: {}", query.sql);
// ########## attempt to call map()
let _ = query.entity.map(Ok(QueryResult {}));
let ret = Vec::new();
Some(ret)
}
}
fn main() {}
The problem is that T in the DbQuery<T> argument in the find method has no idea that T is an Entity type. So we need to tell it:
pub fn find<T>(&self, query: DbQuery<T>) -> Option<Vec<T>>
where T: Entity
{
// ... code here ...
}
This now compiles and runs.
The compiler now knows that T is an Entity of some description, and it can call the map method on it.

Syntax of Rust lifetime specifier

I need help understanding lifetime specifiers. I think I get the concept of lifetimes. I watched Memory, Ownership and Lifetimes. I just think if I could work through this small example it might help me with the syntax of lifetimes. A topic I, to date, find confusing.
use std::collections::HashMap;
fn main() {
pub struct User<'a> {
pub name: & 'a str
}
impl <'a>User<'a> {
pub fn new(uname: & 'a str, pwd: & 'a str) -> User {
User{name: uname}
}
}
pub struct ChatRoom<'a> {
pub name: & 'a str,
pub users: HashMap<& 'a str, User>
}
impl <'a>ChatRoom<'a> {
pub fn new(name: &str) -> ChatRoom {
let users = HashMap::new();
ChatRoom {name: name, users: users}
}
pub fn join(&mut self, user: User) {
self.users.insert(user.name, user);
}
}
let mut room = ChatRoom::new("Test");
let user = User::new("bender","123");
room.join(user);
}
I'm not sure what your exact question is, so I imagine you wanted that code to compile. Check this playground.
Notice that lifetime parameters are part of the type, so you want User<'a> not just User.
use std::collections::HashMap;
fn main() {
struct User<'a> {
name: &'a str,
}
impl<'a> User<'a> {
fn new(uname: &'a str, pwd: &'a str) -> User<'a> {
User { name: uname }
}
}
struct ChatRoom<'a> {
name: &'a str,
users: HashMap<&'a str, User<'a>>,
}
impl<'a> ChatRoom<'a> {
fn new(name: &str) -> ChatRoom {
let users = HashMap::new();
ChatRoom {
name: name,
users: users,
}
}
fn join(&mut self, user: User<'a>) {
self.users.insert(user.name, user);
}
}
let mut room = ChatRoom::new("Test");
let user = User::new("bender", "123");
room.join(user);
}

Resources