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

Add more and better Rust examples #10

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

scuellar
Copy link
Collaborator

@scuellar scuellar commented May 7, 2024

Following our Mattermost discussion about Rust, I've added better examples that more clearly showcase the differences between Rust and CN ownership/mutability models.

@scuellar scuellar requested a review from septract May 7, 2024 16:52
@bcpierce00
Copy link
Collaborator

bcpierce00 commented May 7, 2024 via email

println!("The number is: {}", r); // Safe to use 'r' here
}

fn borrow<'a>(input: &'a i32) -> &'a i32 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be nice to make this be the same as the C code (i.e., write compare in Rust)

/* Rust code:

fn move(){
let x = 42;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you mean to have a string here? Probably better given the name use_string and also numbers are Copy so the assignment might actually work with a number.

}
*/


Copy link
Collaborator

Choose a reason for hiding this comment

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

I know this is not the point here, but would this be better with Block rather than Owned, as allocating and deallocating a string shouldn't really require it to be initialized?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. I was trying to mimic what Rust does and reduce complexity. The comment for the function explains that it creates and initializes the string, but perhaps I should change the name as well to capture that.


#include <stdbool.h> // Include for `bool` type


Copy link
Collaborator

Choose a reason for hiding this comment

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

We are checking for 0, not space (the comment is wrong)


bool use_slices()
{
int fibs[10] = {1,1,2,3,5,8,13,0,21,34};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Comment seems wrong, s1 is returned?

*/

/* Rust code:

Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if it is better to do this example with a slice containing elements of a type that is not Copy, as it would make it more explicit what's happening to the ownership. In particular, you wouldn't use the pattern in the for loop, so you'd have for item in p, and then at the use site you'd have to do a clone (i.e., item.clone()).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could you say more about this, I'm not sure I understand.

let mut x:u32 = 42;
let s2 = &mut x;
let s3:&mut u32 = &mut x; // Fails: can't have two mutable refs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think you want the * here, we are just passing the reference?

unsigned int s = 42;
unsigned int* s1 = &s; // Mutable reference to s
unsigned int* s2 = &s; // Another mutable reference to s

Copy link
Collaborator

Choose a reason for hiding this comment

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

Comments are wrong, the references are s1 and s2

@scuellar
Copy link
Collaborator Author

scuellar commented May 7, 2024

@yav thank you for proof-checking the examples. I've addressed your comments except for the one I commented above.

We could work together to improve that example if you are interested.

@scuellar
Copy link
Collaborator Author

scuellar commented May 7, 2024

Great! Any plans to create some text that might be added to the tutorial?

I don't know how @bcpierce00 and @cp526 want to deal with changes to the text. The new examples have some text explaining the differences I could gather that as tutorial text if that helps.

@bcpierce00
Copy link
Collaborator

bcpierce00 commented May 7, 2024 via email

@septract
Copy link
Collaborator

@scuellar is this still in progress or should we move to merge it?

@bcpierce00
Copy link
Collaborator

What's the status of this stuff? If it's going in the tutorial, then getting it ready in the next few days would be great -- I'd like to do another round of work on the tutorial and get it to some kind of "new level of stable."

@scuellar
Copy link
Collaborator Author

scuellar commented Aug 7, 2024

I was trying to find some time to turn this into a tutorial write up, but it seems like that won't be happening soon and I need to focus more in the CN engineering. I'll clean up the examples and mark this ready for merge.

@bcpierce00 If anyone wants to port the examples into the tutorial I'm happy to serve as a resource. Otherwise I can put this into my stack and get to it when I get more time.

@bcpierce00
Copy link
Collaborator

I'm afraid I'm not a Rust programmer, so I'm not the one to do this bit of writing. What I was imagining was a collection of matched pairs -- here's a Rust program and here's how you achieve the same effect in CN -- with a bit of commentary about what's going on in both the Rust and the CN versions. I do think this would be extremely useful -- both for helping onboard Rust programmers and for broader advertising purposes -- so I hope your cleanup process can at least include leaving enough notes / bullet points that you can recover your thought process later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants