https://github.com/rust-lang/rustlings
I spent the last two weeks going through it and so far the experience has been great. There's a quiz at the end of most chapters.
111–120 of 342 posts
https://github.com/rust-lang/rustlings
I spent the last two weeks going through it and so far the experience has been great. There's a quiz at the end of most chapters.
Earlier quoted context omitted.
"I don't understand why b=a; c=a; Had the same issue a few months ago. The pointer can only be referenced by one variable at a time. If you "move" the pointer from a to b then a is figuratively "used up" because a is now blocked from doing anything with the pointer anymore. I guess, for Rust itself the pointer is still referenced by a, Rust just blocks you from using it. But thinking you moved it to another variable…
if I say a = 2; b = a; c = a; There's no pointer in there, just 2.
Earlier quoted context omitted.
Oh, I've read that. I just maintain that the word 'static is being overloaded here to mean multiple different things. 'static is not the "lifetime of the entire application" when it is used in the context of T: 'static. > It can have references in it! As long as they're bound by 'static. :) ...but it can also have values in it which are not 'static. So is T: 'static, or not? It's arbitrary semantics; ...but my take o…
> 'static is not the "lifetime of the entire application" when it is used in the context of T: 'static. Yes it is. T: 'static means T can live up to the end of the "lifetime of the entire application". > ...but it can also have values in it which are not 'static. I don't follow. Values are indeed bound by 'static. If they weren't we wouldn't be able to pass values to other threads (which can potentially last as long…
'a: 'static is read "'a outlives 'static" [1]
T: 'static is read "T is bounded by 'static" (apparently, although I can't find a reference to it).
The syntax is the same, the meaning is different.
Maybe the 'static part of these two things is the same, but they seem to me to:
- mean different things
- use the same syntax
It is what it is I guess... just confusing as to why it was decided to use the same syntax for these things, instead of something different.
You could argue that adding new syntax makes the language more complicated; but I'm not sure. Does it make it less complicated when you overload the same syntax with multiple different contextual meanings?
Opinions probably vary.
[1] - https://doc.rust-lang.org/reference/trait-bounds.html#lifeti...
Earlier quoted context omitted.
It is dereferencing. It changes `&self` to `self`. The `..` operator wants a value, not a reference.
So the & and * can basically be left out to make ‘self’ a value from the start?
Earlier quoted context omitted.
> 'static is not the "lifetime of the entire application" when it is used in the context of T: 'static. Yes it is. T: 'static means T can live up to the end of the "lifetime of the entire application". > ...but it can also have values in it which are not 'static. I don't follow. Values are indeed bound by 'static. If they weren't we wouldn't be able to pass values to other threads (which can potentially last as long…
Fair I guess; we're just arguing semantics. The point I was originally making is that: 'a: 'static is read "'a outlives 'static" [1] T: 'static is read "T is bounded by 'static" (apparently, although I can't find a reference to it). The syntax is the same, the meaning is different. Maybe the 'static part of these two things is the same, but they seem to me to: - mean different things - use the same syntax It is what…
Earlier quoted context omitted.
> 'static is not the "lifetime of the entire application" when it is used in the context of T: 'static. Yes it is. T: 'static means T can live up to the end of the "lifetime of the entire application". > ...but it can also have values in it which are not 'static. I don't follow. Values are indeed bound by 'static. If they weren't we wouldn't be able to pass values to other threads (which can potentially last as long…
Fair I guess; we're just arguing semantics. The point I was originally making is that: 'a: 'static is read "'a outlives 'static" [1] T: 'static is read "T is bounded by 'static" (apparently, although I can't find a reference to it). The syntax is the same, the meaning is different. Maybe the 'static part of these two things is the same, but they seem to me to: - mean different things - use the same syntax It is what…
T: 'static would happily typecheck with &'a str if 'a: 'static.
T: 'static typechecks:
- OwnedValue
- OwnedValueWithReferences where 'a: 'static
- &'a ReferencedValue where 'a: 'static /// &'static ReferencedValue
- Foo where 'a: 'static /// Foo
...and more.
See the example here: https://play.rust-lang.org/?version=stable&mode=debug&editio...
Notice how bar2 has a &'a str where 'a: 'static and it can be happily passed to bar.
Of course in T: 'static you're subtyping a type and in 'a: 'static you're subtyping a lifetime (which implicitly subtypes the type that it's applied on)... but the ": 'static" part means exactly the same: "the left part of this bound can live up to the end of the application". Whether it's a lifetime, a borrowed type or an owned type does not matter.
> It is what it is I guess... just confusing as to why it was decided to use the same syntax for these things, instead of something different.
Because they are the same.
We could of course separate them into 'noref, 'yesrefbutstatic, 'staticref, etc. But then we'd have a needlessly restrictive std:.thread::spawn that would accept only Owned, or only Owned or only &'static Referenced. The implications are the same, hence they're the same. We wouldn't gain anything and we'd be needlessly restricted.
A simpler way to put it: owned values have an implicit 'static lifetime.
Earlier quoted context omitted.
So the & and * can basically be left out to make ‘self’ a value from the start?
technically yes, but it'll change the semantics in a not useful way. Because if you leave off the & on self then the method won't borrow self (take it by reference) but consume it (take it by value by 'moving' it), so you can't use it any more after calling clone(), which basically makes the method a no-op instead of copying the struct.
Earlier quoted context omitted.
> I don't understand why b=a; c=a; You know how C has a problem with aliases? (multiple variables referencing the same thing)? Which introduce bugs, prevent optimizations, etc? Well, Rust tries to prevent this, and make more explicit (and known to the compiler) when you do this...
I've only had a passing acquaintance with C and C++, so I've never used aliases. I just googled it... why the heck would you copy pointers? That's insane! In my example, a, b, and c were variables, not pointers.
At least in my book pointers are still variables (as in "a pointer variable"), and a variable is any named value, whether it's a scalar or a pointer or a nth-pointer, or what its storage is.
But you mean that in your example there is no way to affect the previous value, right?
>I just googled it... why the heck would you copy pointers? That's insane!
Well, copying values and passing them around would be too costly on memory (for larger structs especially), and would prohibit several techniques.
Earlier quoted context omitted.
Fair I guess; we're just arguing semantics. The point I was originally making is that: 'a: 'static is read "'a outlives 'static" [1] T: 'static is read "T is bounded by 'static" (apparently, although I can't find a reference to it). The syntax is the same, the meaning is different. Maybe the 'static part of these two things is the same, but they seem to me to: - mean different things - use the same syntax It is what…
What confuses me is... why do you think the meaning is different? There is no meaning overload. It's a single meaning. T: 'static would happily typecheck with &'a str if 'a: 'static. T: 'static typechecks: - OwnedValue - OwnedValueWithReferences where 'a: 'static - &'a ReferencedValue where 'a: 'static /// &'static ReferencedValue - Foo where 'a: 'static /// Foo ...and more. See the example here: https://play.rust-la…
If "the left part of this bound can live up to the end of the application" then why is &a not 'a where 'a: 'static? a can live up to the end of the application. &a can live up to the end of the application.
Why is the result "argument requires that `a` is borrowed for `'static`"
fn foo(a: &'a str) { println!("{}", a) }
pub fn main() {
let a = "hello world".to_string();
foo(&a);
}
So I guess I'm going to have to just agree to disagree on this one and bow out of this conversation thread I'm afraid.[1] -- https://play.rust-lang.org/?version=stable&mode=debug&editio...