« first day (2809 days earlier)      last day (662 days later) » 

2:34 AM
hey tuskiomi
 
Hello
 
the way I think about this is to ask myself "who owns the string?"
start with figuring out who's going to own it. is it do_something(), or is it the caller?
every object has to have an owner
 
do_something will own it until return reassigns it to the caller, ideally
as long as do_something doesn't start a thread, that should work.
 
ok yes great
so format!(...) returns an owned String
if you think about it, it's kinda doing the same thing as your function as far as ownership goes
it creates a string "somehow" internally, and then ends up returning a String to you
 
I follow
 
2:39 AM
it gives ownership to you
ok and then you want to pass ownership to your caller
so like format!, you should also return a String
 
why can't a &str come with implicit ownership of the underlying data?
or even explicit ownership
 
right, good question
so... when you return a &str you're saying "I'm not going to let you own this string, but I'll let you borrow it for a while"
but if you're not transferring ownership, that means you still own it
you being do_something
 
so maybe I'm confusing &str with *str?
 
but that doesn't work... cause you're returning... you're not going to exist in a few nanoseconds
*str is very similar
 
or maybe *String? I've never worked with a language with 2 string types...
 
2:42 AM
are you coming from another language? maybe I can make an analogy with C++ or python or JS or whatever
 
Go?
Objective C as well
 
lol dang you hit my weak spot. my go is pretty rusty :P
I think go garbage collects everything, and strings are behind references. is that right?
 
I'm alright with C, if you are as well
Strings are literals in Go, no difference between String and char[]
 
ok let's see. this doesn't translate directly to C cause C's memory management is quite a manual process
 
(except syntax)
 
2:45 AM
you know what, my analogy idea isn't working out
 
VHDL is my best understood language.. idk. longshot
 
let's go back to "why can't a &str come with implicit ownership of the underlying data?"
 
sure
 
I guess that's fundamentally what the distinction is between String and &str
 
so is String a reference, even if outwardly, it isn't?
 
2:47 AM
in garbage collected languages like Go and Java and Python, strings have more bookkeeping information attached to them than in Rust
 
I suppose that would make sense as you can dereference a String.
Right, that makes sense
 
those languages have extra info like reference counts that keep track of who has references to the strings
it makes it easy to pass them around. there's always a bookkeeping cost though. reference counts have to be increased and decreased
and it takes a little extra memory
 
yes.
 
rust says, I can keep track of this stuff at compile time so there's zero runtime overhead
it's super fast and cheap
BUT you have to be more explicit about what exactly you want
if you want to OWN a string, you need a String
then you the owner can let other people BORROW your string, you can give them &strs
or you can transfer ownership by giving them the String, and giving up your String
 
So then why isn't a borrowed value simply denoted by a prefix, like a reference & or a pointer *?
 
2:51 AM
if you let someone borrow your string you are on the hook for keeping that string alive
oh like why isn't it String and &String?
or str and &str?
why does it switch names too?
 
Not exactly
 
well, a borrowed value is denoted by a prefix: &
 
always?
 
aren't Vectors borrowed? or am I mistaken
ie: you can't return vec!(4,5,6)
 
2:54 AM
Thing is an owned object, &Thing is an immutable borrow, and &mut Thing is a mutable borrow
Vec is an owned type
 
huh, and pointers aren't owned?
 
you can return vec![4,5,6], return type would be Vec<i32>
 
er, rather pointers cannot be borrowed*?
 
pointers as in *?
 
yes
 
2:56 AM
yeah pointers are a whole 'nother thing. they're usually an escape hatch from the borrow checker
usually used in unsafe code
they're not normally used in "regular" rust code, they're more for low-level unsafe stuff. interfacing with C code. or sometimes to get around limitations of the borrow checker people will use pointers
 
So then is a rust reference the same as a c++ reference then? it really seems like it's not a reference at all, but something to indicate pass-by-reference and data-owned-elsewhere
 
that's the closest C++ analogue, yeah
C++ doesn't track ownership or borrowing at all though
for example, if you did the equivalent of what you're trying to do in Rust, but in C++
std::string &do_something() {
    return std::string("error!");
}
that will compile. BUT it has the very same ownership problem as the rust code
the std::string is a temporary object that is destroyed when do_something returns
so returning a reference to it is a bad idea
using the return value will almost certainly cause the C++ program to crash
whereas Rust is straight up refusing to let you return a &str
a reference to a string that won't live long enough
 
but wait, if I change my example to
`return Err("error! mynum is divisible by 12!")`

That's perfectly acceptable per the compiler
it has no issue with string literals
it's only when I format! something
 
yes indeed. right you are
 
so who owns that string?
 
3:03 AM
love it. the perfect question
well let me turn it around. can you think of who owns it?
it is indeed owned, and it's not by do_something()
like where in your computer is that string literal stored
 
If I think about what the compiler would do, I think it would likely inline it to a global static memory space, and... clone(?) it when the function returns with new ownership data?
 
yes that's it, it's in a global static memory space
it's "owned" by your executable, so to speak
the executable is not going anywhere, so these static strings are never dropped
 
does that mean that if the caller of the function edits the result via string manipulation, then every return thereafter will be tainted?
 
yeah
the saving grace is that &str is an immutable reference. it doesn't let you modify the string
you would need a &mut str
 
oh. right, and most string manipulation makes use of cloning anyways.
so then I take it that a &mut string cannot be literal?
 
3:09 AM
right
 
is that best practice, or is that enforced?
 
if you try to do let s: &'static mut str = "literal"; it won't compile
it's enforced. the only way around it is with unsafe
 
gotcha. so.. there's really no way to manually assign who's borrowing what
 
there are ways. it's more than I can explain here
but you may have seen things like Rc and Arc -- smart pointers
or things like RefCell and Mutex -- wrappers that allow for interior mutability
 
I've heard they're related to Cells, but no clue how
 
3:14 AM
yeah I'd say read or re-read the chapters in the Rust Book about this stuff
 
One last question. can I make a variable un-borrowable?
 
tbf it took me quite a while to get comfortable with cells and arcs and whatnot
hmmm
 
(besides wrapping it in another data structure)
 
why do you ask?
 
because I like my C references dangit!
haha
 
3:16 AM
hehe
what do you mean by un-borrowable?
 
hmm
That whoever is borrowing the value is the owner, and passing the value to methods temporarily reassigns ownership until the method exits.
 
&mut references are essentially that, if I understand you right
a &mut reference lets someone else use your object
and they have exclusive access to it
nobody else can borrow it until they give up the &mut borrow
that's like temporary ownership
 
I was reading about those today...
and the way they work internally, you'd think that they'd be written as &mut<T> instead of &mut T
mut is treated as a special type-keyword hybrid it seems
anyway, just some random thoughts.
Thank you for the help, I understand a little better.
 
cool, hope that helps
it took me a long long time to get comfortable with this stuff
 
With the way I work.. I don't really get comfortable as much as I get complicit
 
3:23 AM
these concepts just don't exist in any other language
heh!
 
^ You can do lifetimes in VHDL, though they're not annotations, and they're funcitonal
so maybe not lifetimes, then :S
Anyway, thank you again for your help!
 
yeah cheers!
 
I just want to hop in here and say that I loved reading the discussion here from afar. @JohnKugelman, you've been an amazing mentor. And @tuskiomi, you've been very receptive and attentive, especially nice to see considering the comments in your question seemed... frustrating. Glad you guys were able to talk things through.
3
 
3:59 AM
@kmdreko aww <3
 
 
10 hours later…
2:22 PM
Ze hammer ist gud
 
But if your only tool is a hammer then every problem looks like a nail.
 
2:49 PM
Good thing I have other tools then.
 
 
9 hours later…
11:27 PM
@tuskiomi I used to use VHDL, but I'm not sure what you're referring to here. Care to elaborate briefly? I mean, even if it's not "lifetimes" per se, I'm interested in what feature you think is similar or related.
 

« first day (2809 days earlier)      last day (662 days later) »