I posted this on your Snoyberg blog

11 views
Skip to first unread message

Mitch Raful

unread,
Dec 19, 2020, 11:13:01 AM12/19/20
to Begin Rust
Hi, bought the book, but assume that allows me to post a question I left on the old Disqus site:

Wow, I realize this post is 2 years old but hope Michael occasionally checks it. (He better I also bought the ebook to show my appreciation for this blog style tutorial). Since I am learning I came up with this way to solve building the frame:


let mut top_bottom = |dir: &str| {
if dir == "horiz" {
write!(fmt, "+");
for _ in 0..self.frame.width {
write!(fmt, "_");
}
write!(fmt, "+\n");
} else {
for row in 0..self.frame.height {
write!(fmt, "|");
for _ in 0..self.frame.width {
write!(fmt, " ");
}
write!(fmt, "|\n");
}
}
};

top_bottom("horiz");
top_bottom("vert");
Ok(top_bottom("horiz"))
}

My question? Is this a Rust acceptable way to do so. I ask because I am want to understand Rust and not just copy. I also ask because while this may work, Rust programmers may find this NOT idiomatic to Rust. I have found most languages have multiple way to solve things but one is frowned upon. For example, working with ReactJS, I found that React actually makes is fairly easy to manipulate the DOM directly, but the React creators and maintainers frown up on it because React should manipulate the DOM through its virtual DOM.


Let me know your thoughts.


Thanks!

Michael Snoyman

unread,
Dec 20, 2020, 1:16:20 AM12/20/20
to Mitch Raful, Begin Rust
Hey Mitch,

I responded on the Disqus comments too, available here: http://disq.us/p/2dxareo. But for those reading on the mailing list, I'll copy-paste my answer:

This looks mostly good to me, just a few thoughts:

* I'd recommend using an `enum` for direction instead of a `&str` to get a bit more compile time safety and a slight performance boost.
* Error handling isn't handled correctly here. Each `write!` call generates a `Result`, which is being thrown away by the semicolon. The `Ok(top_bottom("horiz"))` at the end essentially says "ignore all errors that occur." Using the question mark/try operator is a better approach.
* Also, if desired, you could use `writeln!(fmt, "+")` instead of `write!(fmt, "+\n")`, though I'm honestly not certain if there's a meaningful difference.

On that second bullet: I'm planning on recording a short video on proper error handling in Rust and publishing on the FP Complete YouTube channel in the next few days, in case that explanation will be helpful.

Hope that helps!
Michael

--
You received this message because you are subscribed to the Google Groups "Begin Rust" group.
To unsubscribe from this group and stop receiving emails from it, send an email to begin-rust+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/begin-rust/2b898221-9c30-4efd-8f5e-9d343f85b9b8n%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mitch Raful

unread,
Dec 20, 2020, 9:16:12 AM12/20/20
to Begin Rust
Hey thanks!   And, I 'm just starting Crash Course Lesson 3, Iterators and Errors where I think you address throwing away the Error. I'll have check on how to use an enum in place of the &str.
Reply all
Reply to author
Forward
0 new messages