Questions tagged [rust-rustlings]

8 questions
2
votes
1 answer

Rustlings slice primitives

One of the rustlings exercises on primitive is about slices. When I tried to solve this exercise I started by using the [start...end] syntax to take a slice into the given array as such fn slice_out_of_array() { let a: [u8; 5] = [1, 2, 3, 4,…
Nicola Pedretti
  • 4,831
  • 3
  • 36
  • 42
1
vote
1 answer

Rustlig if2 trying to convert 1 to "1" inside the function foo_if_fizz

I'm new to rust and I'm looking for a way to convert 1 to "1" at compile time; pub fn foo_if_fizz(fizzish: &str) -> &str { if fizzish == "fizz" { "foo" } else { &(1.to_string()). <-- returns a reference to data owned by the…
1
vote
1 answer

rustlings: hashmap3.rs expected `()`, found struct `Team`

I'm learning rust using rustlings. I was trying to implement a solution in a functional way (rustlings only asked for the imperative way). I'm running into the following compiler error and haven't been able to figure this out since 2 hours. Please…
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
0
votes
1 answer

Rust visibility from child module to outside

I'm learning about Rust, and trying to figure out the visibility rules for modules. I have the following code: fn x() -> u8 { 5 } struct Person { name: String, } mod example { use super::{x, Person}; pub fn foo(person: &Person) { …
DACUS
  • 33
  • 1
  • 5
0
votes
1 answer

Rustlings Quiz2: Why is i of type &usize instead of usize

I am new to Rust and I am attempting the Rustlings quiz2 problem. Below is the whole code for reference. // quiz2.rs // This is a quiz for the following sections: // - Strings // - Vecs // - Move semantics // - Modules // - Enums // Let's build a…
jcleow
  • 57
  • 1
  • 5
0
votes
0 answers

Rust pattern match for "not equal" to a literal

I'm a rust newbie. This is not a homework question. I know I can match on literals like the first match below. I've indicated where I need help below with vvvv. I'm checking if a divides b completely (if it does return a/b, else return err…
Dheeraj Bhaskar
  • 18,633
  • 9
  • 63
  • 66
0
votes
0 answers

rust: integer multiplication in match

Hi I am learnig rust and doing the excercise rustlings/itertors3.rs My first implementation of fn divide not behaves as I expected. Could Someone explain me why? My first attempt was: // Calculate `a` divided by `b` if `a` is evenly divisible by…
murku
  • 1
  • 2
0
votes
1 answer

Method `push_str` returns () instead of String

I'm trying to learn rust via rustlings and I'm encountering this weird error. I understand that it modifies self in place but why does it return a unit () instead of the modified String impl AppendBar for String { // TODO: Implement `AppendBar`…
Starfish
  • 31
  • 3