

Answers to StackOverflow's top Rust programming questions explained
source link: https://www.youtube.com/watch?v=Flf4ezLWw1E
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Answers to StackOverflow's top Rust programming questions explained
If you're learning Rust, you've probably wanted answers to these questions. The video is a recorded live stream with lots of discussion.
Timeline
00:00 Welcome and introduction 02:29 What are the differences between String and &str? 18:40 Why do we need an ampersand (&) when dealing with str? 19:40 Language Server Protocol (LSP) support in Rust 21:40 How to create a function that accepts a String or a str 35:26 Why doesn't println! work in Rust's unit tests? 39:20 Why does the Rust compiler not optimize code assuming that two mutable references cannot alias? 44:40 How to disable unused code warnings in Rust? 47:20 How to I print the type of a variable in Rust? 52:07 Do I have any tips for starting with Rust programming from scratch? 55:50 What are "generics"? 58:17 What is a "trait"?
Here are direct links to the questions
SO questions tagged rust https://stackoverflow.com/questions/t... What are the differences between String and &str? https://stackoverflow.com/q/24158114/... Why doesn't println! work in Rust's unit tests? https://stackoverflow.com/q/25106554/... Why does the Rust compiler not optimize code assuming that two mutable references cannot alias? https://stackoverflow.com/q/57259126/... How to disable unused code warnings in Rust? https://stackoverflow.com/q/25877285/... How do I print the type of a variable in Rust? https://stackoverflow.com/q/21747136/...
About the speaker
Tim McNamara is the author of Rust in Action https://manning.com/books/rust-in-action. Follow Tim on Twitter https://twitter.com/timClicks and watch future tutorials live on Twitch https://www.twitch.tv/timclicks
Show lessShow more
16 Comments
What you said about strings isn't entirely accurate.
fn greet(name: &str) { println!("Hey!, {}", name); } Will accept both String and &str as a parameter without the need for a call to AsRef<str> as such:
fn main() { let jesus = String::from("Jesus"); let dave = "Dave"; greet(&jesus); // Note that you have to borrow a 'String' greet(dave); //Note that you don't have to borrow explicitly here }
Also, I strongly recommend (in fact I'd even go as far as to say) that you don't allocate Strings willy nilly just because. Rust is a systems language and, I'd argue, you are learning it or are interested in it because you want to make performant software, and String allocations (and Vectors, and Box, and HashMaps, etc.) are expensive and should be used with care, unless of course you don't care about performance or memory usage like this: fn greet<T: Into<String>>(name: T) { println!("Hey!, {}", name.into()); // potentially very expensive copy and allocation of data!!! Warning!!! } Or: fn greet<T: ToString>(name: T) { println!("Hey, {}", name.to_string()); // warning!!! The same as above!! } Cloning heap objects should not be your go to when writing software that's supposed to be performant, but as a beginner learning Rust I guess it's okay, but you should never reach for cloning objects unless absoultely necessary. In fact, I'd even say, that if you're cloning a lot in your codebase then you should try to see if there's a better way to structure your program than what you're currently doing.
References (or borrows in Rust lingo) should be the absolute first thing you reach for when chosing how to pass parameters (&T) and copy or clone cheap types like integers and floats and fat poitner types (like &str &dyn Trait, etc.).
And just to reiterate: Allocations? Not bad when needed but as systems level devs we should try to use the stack as much as we can and only touch the heap when absolutely necessary.
TL;DR: Don't allocate everything on the heap (Box, Vec, String, etc.) because it's easy and 'quick', reach for shared references (&T) first or static (stack arrays or a SmallVec, etc.) data structures before everything else.
Read more 1 week ago
Read more 1 week ago (edited)
Recommend
-
15
Top 100 Python Interview Questions You Must Prepare In 2020 Last updated on May 07,2020 1M Views Aayushi Johari A technophile who likes writing...
-
14
Top 50 Tableau Interview Questions You Must Prepare In 2020 Last updated on Oct 25,2020 387.4K Views Reshma Ahmed Reshma is a tech-savvy professional...
-
11
Top 50 React Interview Questions You Must Prepare In 2020 Last updated on May 04,2020 620.6K Views Swatee Chand Research Analyst at Edureka...
-
19
Top 50 Salesforce Interview Questions And Answers You Must Prepare In 2020 Last updated on Jul 19,2020 374.5K Views Vardhan Vardhan is a technology enthus...
-
26
Azure Interview QuestionsGartner in its latest release of Magic Quadrant, has listed Azure as the second most dominating cloud provider for Infrastructure as a Service. What does this mean? Obviously, there are a lot...
-
13
Introduced in 2013, Docker hit the IT industry. Soon it turned out to be a big hit by the end of 2017 with 8 billion container image downloads. Increasing demand for docker showed an exponential increase in job openings. Go ahead and take adv...
-
11
Top DevOps Interview Questions You Must Prepare In 2020 Last updated on Jan 14,2020 696.9K Views 1 / 9 Blog from DevOps Interview Questions Are you a DevOps engineer or thinking of getting in...
-
17
With the boosting popularity of JavaScript, the world has seen a revolutionary change in web development. The process and techniques of web designing have taken a dramatic turn. JavaScript running on the server and in the browser has opened n...
-
7
C# Programming Questions and Answers © 2009 - 2021 by IndiaBIX™ Technologies. All Rights Reserve...
-
4
C++ Programming Questions and Answers
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK