cleancut / ultimate_rust_crash_course Goto Github PK
View Code? Open in Web Editor NEWRust Programming Fundamentals - one course to rule them all, one course to find them...
License: MIT License
Rust Programming Fundamentals - one course to rule them all, one course to find them...
License: MIT License
Hi, just doing the excersise G-COLLECTIONS_ENUMS and I'm stuck on the last point 3, looping through shots. I'm not really sure how to fix this.
here is my code:
// Silence some warnings that could distract from the exercise
#![allow(unused_variables, unused_mut, dead_code)]
// Someone is shooting arrows at a target. We need to classify the shots.
//
// 1a. Create an enum called `Shot` with variants:
// - `Bullseye`
// - `Hit`, containing the distance from the center (an f64)
// - `Miss`
//
// You will need to complete 1b as well before you will be able to run this program successfully.
enum Shot {
Bullseye,
Hit(f64),
Miss,
}
impl Shot {
// Here is a method for the `Shot` enum you just defined.
fn points(self) -> i32 {
match self {
Shot::Bullseye => 5,
Shot::Hit(x) => {
if x < 3.0 {
2
} else {
1
}
},
Shot::Miss => 0
}
// 1b. Implement this method to convert a Shot into points
// - return 5 points if `self` is a `Shot::Bullseye`
// - return 2 points if `self` is a `Shot::Hit(x)` where x < 3.0
// - return 1 point if `self` is a `Shot::Hit(x)` where x >= 3.0
// - return 0 points if `self` is a Miss
}
}
fn main() {
// Simulate shooting a bunch of arrows and gathering their coordinates on the target.
let arrow_coords: Vec<Coord> = get_arrow_coords(5);
let mut shots: Vec<Shot> = Vec::new();
// 2. For each coord in arrow_coords:
//
// A. Call `coord.print_description()`
// B. Create the correct variant of `Shot` depending on the value of
// `coord.distance_from_center()`
// - Less than 1.0 -- `Shot::Bullseye`
// - Between 1.0 and 5.0 -- `Shot::Hit(value)`
// - Greater than 5.0 -- `Shot::Miss`
for coord in arrow_coords.iter() {
coord.print_description();
if coord.distance_from_center() > 1.0 {
shots.push(Shot::Bullseye);
} else if coord.distance_from_center() >= 1.0 && coord.distance_from_center() <= 5.0 {
shots.push(Shot::Hit(coord.distance_from_center()))
} else {
shots.push(Shot::Miss);
}
}
let mut total = 0;
// 3. Finally, loop through each shot in shots and add its points to total
for shot in shots.iter() {
total += shot.points();
}
println!("Final point total is: {}", total);
}
The error is pointing at line toal += shot.points()
saying
move occurs because
*shot
has typeShot
, which does not implement theCopy
trait
I know what it means, but I don't know how to get it working. Thank you!
Te dejo el nuevo xD, avísame que cierro este issue de mierda. Lauu#8445
Hey there,
I'm working through the first exercise and reviewing the answers. I see there's a reference to a loaded
variable, but I don't see this mentioned in the readme instructions. Perhaps this was meant to be ready
? Small nitpick but thought you might like to know! If not, no worries. Have a good one 👋
I am leaning rust and taking your course (on skillshare if you are currious)) and everything have been super clear so far, the only thing I haven't liked is that you say
// Time for some fun with threads and channels! Though there is a primitive type of channel
// in the std::sync::mpsc module, I recommend always using channels from the crossbeam crate,
// which is what we will use here.
Without giving a reason why you prefer the crate over the std library. Do you have an article (or know of one) which goes into why crossbeam is better than the std library, or maybe the reason is so simple you can summerize it in a sentence or two?
The video chapters/lessons never mentions channels either, so as I don't know what channel::
is I have a hard time even understanding it and its user cases.
I just wrote fn add(a: &i32, b: &i32) -> i32 {a+b}
which works correctly, is that intended? There seems to be no manual dereferencing needed, has that changed since your created the exercises?
Or is the correct answer *a+*b
with explicit dereferencing?
// Challenge: Write a function "add" that takes *references* to two integer arguments,
// dereferences them and adds them together, and returns the result.
//
println!("1 + 2 = {}, even via references", add(&1, &2));
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.