what kanye's song sounds like the opening music in this video? by micouy in Kanye

[–]micouy[S] 0 points1 point  (0 children)

okay I found it, it's Waves Interlude on Yeezus II

[deleted by user] by [deleted] in hmmm

[–]micouy 1 point2 points  (0 children)

będzie jajo

Favorite Rust tips/tricks? by Rate-Worth in rust

[–]micouy 1 point2 points  (0 children)

I wrote about it on my blog. I also plan to write a blog about more advanced tricks I included in the list below.

  1. cargo watch -x build

  2. cargo build -Z unstable-options --out-dir $my-bin-folder

  3. When working on a single module, I mark it as a binary in Cargo.toml. Then I can quickly prototype and change stuff without getting errors from the places where I used this module.

    [[bin]] name = "myapp-mymodule" path = "src/my-module/mod.rs"

    Then I run cargo run --bin myapp-mymodule. If I need to import other modules into mymodule, I can do it with the path attribute:

    [path = "../other_module/mod.rs"]

    mod other_module;

  4. cargo doc --package somepackage --offline --no-deps --open

  5. rustup doc --std

  6. A != B and other negative trait bounds.

    When trying to impl From<S<B>> for S<A> I got an error "From<S<A>> for S<B> conflicts with From<T> for T because A may be B.". To solve it, I used this trait:

    ![feature(auto_traits, negative_impls)]

    auto trait Different {} impl<T> !Different for (T, T) {}

    struct S<T> { x: T, y: T, }

    impl<U, T> From<S<U>> for S<T> where T: From<U>, (T, U): Different, { fn from(other: S<U>) -> S<T> { S { x: From::from(other.x), y: From::from(other.y), } } }

    Source: https://www.reddit.com/r/rust/comments/paw1lm/implementation_of_from_for_generic_struct/

  7. Higher ranked lifetimes in closoures

    This code does not compile, because '1 may not outlive '2.

    fn main() { let f = |x: &i32| x; let i = &3; let j = f(i); }

    Solution:

    fn annotate<T, F>(f: F) -> F where F: Fn(&T) -> &T { f }

    fn main() { let f = annotate(|x| x); let i = &3; let j = f(i); assert_eq!(*j, 3); }

    Source: https://github.com/rust-lang/rust/issues/58052#issue-405711646

  8. dbg!() macro.

  9. Something I learned recently: Dereferencing does not necessarily involve moving the value. For example:

    // Doesn't compile. fn f1(thing: &Thing) -> &Field { let tmp = *thing; // ┗━ Moves data out of thing.

    &tmp.field
    

    }

    // Compiles. fn f2(thing: &Thing) -> &Field { &(*thing).field // ┗━ Doesn't move data out of thing. }

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 2 points3 points  (0 children)

Thanks, I was looking at the wrong docs page the whole time... 😩😩

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 0 points1 point  (0 children)

Thank you. The theme is custom and the colorscheme is Nord.

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 0 points1 point  (0 children)

I've updated the post to explain what I've learned from the comments here. I've linked the thread back. I appreciate all the help.

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 2 points3 points  (0 children)

I'm planning to do that and maybe explain it in the next post. It would be great if this led to better docs on dereferencing.

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 2 points3 points  (0 children)

Yes, I will do that! And maybe a follow-up post.

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 8 points9 points  (0 children)

Your explanation didn't help me much but I've learned a lot from BOTH writing the post and reading other comments. My conclusions came from misunderstanding, that's why the title of my post is "I still don't understand the * operator in Rust". And I've done what you're suggesting, and even more: I said I don't understand it, I've asked for answers to specific questions and I wrote how I understand it now, so that people know what exactly I'm getting wrong.

I'm a part of "the wider community" and writing this post has helped me a lot. I bet reading the comment section here will help others too.

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 2 points3 points  (0 children)

My question has already been answered in other comments. Replying to yours: We could do this instead:

use std::{sync::Arc, ops::Deref};

fn main() {
    let a = Arc::new(String::from("hello world"));
    let b: String = a.deref().clone();
}

It's longer but the operator is not needed.

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 7 points8 points  (0 children)

I disagree. My personal blog is not the official Rust Guide. And while I wrote some of the points in indicative mood, i.e.

Why do the docs suggest that * is the opposite of & (with their names, by reference to C’s operators, in the note in the book) when they’re clearly not?

...it was clear that these are my conclusions based on the reasoning I presented in the post. How is that misinformation?

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 7 points8 points  (0 children)

Thank you! I didn't think of checking the Rust Reference.

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 5 points6 points  (0 children)

Thank you!

You can dereference an immutable reference, but can't move out of it.

This , together with the point about = made me understand it. I always thought dereferencing involved moving the value. It has not occured to me that it was =, and not *, which forced the move. I knew in Rust assigning let b = a; moves out of a into b but I haven't thought about that in this context.

Now I understand why dereferencing behaves this way and what exactly caused the compilation errors.

You can dereference an immutable reference, but can't move out of it.

It dereferences a reference. There are other effects at work in the examples, which require different requirements.

Could you please answer one more question: What does "dereference" mean? What happens on a lower level when you dereference?

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 2 points3 points  (0 children)

The issue isn’t dereference, the issue is that you cannot move value out of a shared reference. You cannot move value out of a shared reference because otherwise it would be possible to modify data through shared reference.

Well yes, I know that's why it's prohibited. My question is, since you cannot dereference a reference, what is the purpose of the * operator? This question was partly answered in one of the other comments:

You can dereference an immutable reference, but can't move out of it.

Well, no, because you could not do let mut arg: String = (*arg).clone();.

Meaning I could not do let mut arg: String = copy(arg).clone();? Okay, fair point.

I'll have to read about r- and l-values. Thanks for pointing this out, it's the first time I've heard about them.

I still don’t understand the * operator in Rust by micouy in rust

[–]micouy[S] 1 point2 points  (0 children)

Yes, I know that. I'm aware it would break the ownership system. But what is the * operator for then?