About objects restrictions

What happens

I’m trying to solve a CodeAbbey exerciser but yesterday somebody remembers me that I can’t use OOP approach to solve coding exercises and that creates an existential crisis for me because now I don’t know how to use Rust possibilities to solve this exercise.
At this moment I’m re-writing the code and I would like to use Rust structs but… are they forbidden?

What do you understand or find about that problem

The submission guide says that classes are not allowed and I need to abstract using functional programming, so I update and create the structs using a functional programming abstraction.
Reading Rust documentation, it says that objects are built from structs and impl… but a struct is not an object… so, I don’t know what to think…

You make any workaround? What did you do?

I create a code that I will explain in the next section


let’s suppose:

  1. I create a Rust struct:
    struct MyStruct {
    age : i32,
    name: String,

  2. Then I create a list of structs
    let mut kids = Vec::new();
    kids.push(MyStruct{age: 7, name: pepito})

  3. In a for loop I change some value using the return of a function:
    fn next_age(age: i32){
    age += 1
    for mut i in kids{
    i.age = next_age(i.age)

I need help with

I need to know if what I’m doing in the example above is forbidden and why? how can I recognize if what I’m doing is not valid?? when an object its an object?? I couldn’t sleep thinking about it…

I do not know anything about rust but I can give you some tips. Functional programming as its name says it is a style in which you focus on programming only with functions, even the smallest instruction should be in a function, so you should apply the style in The language you have chosen, which in this case is rust as you mentioned, avoid global vars and I will leave you this video that may soon be of help to understand better this style and that you can apply it in the language.

Yes, the main idea of functional programming it’s no mutations, always use constants, with that you should use functions to get the values, that means that for/while cycles are not allowed in most of the times, you should learn how to use recursions, and make it with a thinking that you can’t re-use vars, you must re-use functions.

For example, you want to make an array with prime numbers.

In this case, you can’t push things to your array, because you will work with constants, you should make a function that returns you that array completed.

function five_primes(arr, start) {
  if len(arr) === 5 {
    return arr
  } else if isPrime(start) {
    return five_primes(concat(arr, start), start + 1)
  } else {
    return five_primes(arr, start + 1)

function main() {
  const primes = five_primes([], 1)
  print("these are five prime numbers: ", primes)

That couldn’t be work because is an example function, but I hope that you get the idea with that

If you want to read a lot of examples about functional programming you should see the solutions that we have in the repo on TypeScript, Erlang, Clojure, etc

Ok, but what in the case that i need to store users information? and that information is giong to change in time… I Don’t know, information like health condition? and at the end I need to print sick users name?
Can I create structs that changes it values inside a function but the function return a new struct?

Functional programming is a different paradigm, you must figure out how to solve those problems with your own imagination and creativity, in that case, you can work with the user name as a param in a function that do all things that you need to that user:

function get_user_status(user) {
  .......... // Here you can make your logic to know if is sick or healthy
  if sick {
    return [user, "sick"]
  } else {
    return [user, "healthy"]

function main() {
  const users= [user1, user2, user3, user4]
  const users_health = users.map((user) => get_user_health(user)) //now you have every user health
  ..... // You can do whatever you want after that

You can work with an object too changing the return from [user, "status"] to {user: '<user>', health: '<status>'}

Functional programming is a way to think about how you are abording the problem and is your responsibility to figure out in each challenge that you are affronting.

Ahhhhhh ok, ok, ok… I got it!! ohhh thank you too much… Then I guess that what I did today is ok :slightly_smiling_face:

use objects don’t mean mutate objects, you can keep that in mind, and always you can use as an example the challenges on typescript because we have a plugin that forces the use of functional programming on that lang