WARNING: student did not upload Cargo.lock. This may cause build errors.
Compiling health_statistics v0.1.0 (/mnt/exercism-iteration)
warning: function cannot return without recursing
--> src/lib.rs:12:5
|
12 | pub fn new(name: String, age: u32, weight: f32) -> Self {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
13 | return User::new(name, age, weight)
| ---------------------------- recursive call site
|
= help: a `loop` may express intention better if this is on purpose
= note: `#[warn(unconditional_recursion)]` on by default
warning: `health_statistics` (lib) generated 1 warning
warning: `health_statistics` (lib test) generated 1 warning (1 duplicate)
Finished test [unoptimized + debuginfo] target(s) in 2.34s
Running unittests src/lib.rs (target/debug/deps/health_statistics-280f60203f84a21b)
Running tests/health-statistics.rs (target/debug/deps/health_statistics-bc6ead1c16acd366)
thread '
thread 'test_name' has overflowed its stack
fatal runtime error: stack overflow
error: test failed, to rerun pass `--test health-statistics`
Caused by:
process didn't exit successfully: `/mnt/exercism-iteration/target/debug/deps/health_statistics-bc6ead1c16acd366 -Z unstable-options --include-ignored --format json` (signal: 6, SIGABRT: process abort signal)
#This gives me a weird error.. what am i doing wrong
1 messages · Page 1 of 1 (latest)
Did you mean to write User { name, age, weight }?
Otherwise the function just calls itself
ohh
i need braces
still error
nvm
compiles now
how do i get it to return a field of the struct
that was my guess
its erroneous
are you sure you need to make these fields read only outside that module? thats the only reason why youd want getters really
I assume the error is like "expected &str, found String"? or "cannot move from behind a reference"
have you ran cargo check for the full error? maybe it has a tip
also
-code
Please post your code examples and compiler output with code fences (```) around them. Example:
```rust
let (x, y) = (0, 42);
println!("Position at {}, {}", x, y);
```
let (x, y) = (0, 42);
println!("Position at {}, {}", x, y);
If the snippet is long or you want to demonstrate something, consider sharing it through the playground: https://play.rust-lang.org/ or https://www.rustexplorer.com/ or https://paste.rs/web.
Please avoid sharing screenshots of your code, as they're not very accessible. Using code fences or a shared snippet makes the code more readable and allows those helping you to copy-paste the code to help explain things.
this part specifically for you
Please avoid sharing screenshots of your code, as they're not very accessible. Using code fences or a shared snippet makes the code more readable and allows those helping you to copy-paste the code to help explain things.
ok
pub struct User {
name: String,
age: u32,
weight: f32,
}
impl User {
pub fn new(name: String, age: u32, weight: f32) -> Self {
return User {name, age, weight}
}
pub fn name(&self) -> &str {
return self.name
}
pub fn age(&self) -> u32 {
return self.age
}
pub fn weight(&self) -> f32 {
return self.weight
}
WARNING: student did not upload Cargo.lock. This may cause build errors.
Compiling health_statistics v0.1.0 (/mnt/exercism-iteration)
error[E0308]: mismatched types
--> src/lib.rs:17:16
|
16 | pub fn name(&self) -> &str {
| ---- expected &str because of return type
17 | return self.name
| ^^^^^^^^^
| |
| expected &str, found String
| help: consider borrowing here: &self.name
For more information about this error, try rustc --explain E0308.
error: could not compile health_statistics due to previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile health_statistics due to previous error
Do exactly what the error message suggests.
struct.field, unless told otherwise, tries to move field out of struct, which
- is a type error here,
Stringisn't&str - would be illegal anyway, can't move out of a
&
the error just says that it expected a reference to a string rather than a string
so i have now put a ‘&’ in front of thr ‘self.name”
whats the correct syntax
i think this is correct..
oh wait
it ran
ok all good
help: consider borrowing here
&self.name
also, this is not an error fix but just style:
don't use return here; it's not necessary except when you want to return early from a function
oh okay