-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
Conversation
println!("The number is: {}", r); // Safe to use 'r' here | ||
} | ||
|
||
fn borrow<'a>(input: &'a i32) -> &'a i32 { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
} | ||
*/ | ||
|
||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 | ||
|
||
|
There was a problem hiding this comment.
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}; |
There was a problem hiding this comment.
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: | ||
|
There was a problem hiding this comment.
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()
).
There was a problem hiding this comment.
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. | ||
|
There was a problem hiding this comment.
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 | ||
|
There was a problem hiding this comment.
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
@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. |
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. |
@scuellar is this still in progress or should we move to merge it? |
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." |
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. |
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. |
Following our Mattermost discussion about Rust, I've added better examples that more clearly showcase the differences between Rust and CN ownership/mutability models.