Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Stacks and Queues in Rust. #924

Conversation

PeanutbutterWarrior
Copy link
Contributor

I've done the same as the Java and Typescript examples code-wise, however I've added minimal code comments. One is Rust specific, pointing out where a reference is returned instead of the value due to the borrow checker. I've also added comments after the print statements in the test to show the output of them.

Copy link
Contributor

@henrikac henrikac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks fine to me

if self.list.len() > 0 {
Some(&self.list[0])
} else {
None
Copy link
Contributor

@ishwar00 ishwar00 Nov 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it safe to return None as front element when Queue is empty?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean safe?
You're returning an Option<> so returning a None is one of the expected values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay safe may not be correct word here.
I thought like, say i was doing something like Q.front() * 5, then returning None would not be right thing to do, right? instead executing panic!("Attempt to access element from a empty Queue") may be helpful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't do Q.front() * 5 anyway with an Option. panic! is not an exception, you are crashing the application to say something bad happened (and implied unrecoverable, which is not the case here).

Plus panic! only works at runtime, which is kinda late to warn on possible problems.

When you write a public facing function, you have to define what the function takes and returns. You could only return T, and crash/exception/error handle.
But Option says, "ok we can have either a legit value or None".
That None can mean many things, but it describes that no valid data can be returned. And you don't want your user to fall into cases where you get weird/undefined behaviours, so being explicit helps a lot. And the programmer than has to deal with the different cases (or can take shortcuts if he knows that special cases can't happen).

You don't really care what the programmer does, what you do care about is correctly defining and documenting what your function does, deosn't do, and special cases. And there is always several ways to do that, but I think in this case having Option does the job well enough.

You can see in the standard library for example here that Optional is also used in similar cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for being so kind.
You completely make sense.

@Amaras Amaras added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) lang: rust Rust programming language labels Nov 27, 2021
Copy link
Contributor

@ShadowMitia ShadowMitia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good overall. Could you just add a Cargo.toml and I think we should be good.

I think this should be enough, but feel free to edit it.

[package]
name = "stack_and_queue"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]


[[bin]]
path = "./Queue.rs"
name = "queue"

[[bin]]
path = "./Stack.rs"
name = "stack"

@PeanutbutterWarrior
Copy link
Contributor Author

Like I've said before, if the name of the built binary isn't main then it won't be copied into the build directory by SConscript. Cargo can't build both files because into main, so I think that we should leave it to be compiled with rustc.

SCons can't do multiple files from the same chapter yet, which will need fixing for #950 as you pointed out there. I think it might be best to hold off merging this PR (and #950) until we work that out.
We could also add a special case SConscript file here to deal with this, but as it's probably going to crop up again elsewhere we should make it work by default

@ShadowMitia
Copy link
Contributor

Right I forgot about that. #950 is for interpreted languages, it doesn't really make sense to modify Rust stuff there. It's best to find a solution separately (unless it's convenient all at once I guess).
I'll approve this PR then, @Amaras what do you suggest we do regarding the scons stuff?

@Amaras
Copy link
Member

Amaras commented Nov 29, 2021

I'll approve this PR then, @Amaras what do you suggest we do regarding the scons stuff?

Since both files have a main function, the smartest move in my opinion would be to put a SConscript with two distinct rustc targets (say build/rust/stack and build/rust/queue).

We'll merge once this is done, since there already is an approval.

@PeanutbutterWarrior PeanutbutterWarrior force-pushed the implement-stacks-queues-rust branch from 4c95e98 to cadad08 Compare November 29, 2021 21:46
@PeanutbutterWarrior
Copy link
Contributor Author

I've had to rebase in order to get the latest SCons updates, as this branch was created before most of it. No conflicts had to be resolved. Git was confused, so I had to force push. I've added the SConscript file and checked that it builds

Copy link
Member

@Amaras Amaras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, the SCons part looks good to me.

We'll just need to merge the master branch correctly and it'll be good.

@leios
Copy link
Member

leios commented Dec 2, 2021

Is there anything holding this PR back? I think it's mergable, but am not sure why we waited before

@leios leios merged commit aa83fec into algorithm-archivists:main Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) lang: rust Rust programming language
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants