FuelLabs/sway-libs

`push` symbol could not be found

Closed this issue · 4 comments

Describe the bug

This piece of code cannot compile:

contract;

use string::String;

abi MyContract {
    fn return_dynamic_string(l: u8) -> String;
}

impl MyContract for Contract {
    fn return_dynamic_string(l: u8) -> String {
        let mut string = String::new();
        let mut i = 0;
        while i < l {
            string::push(i);
            i = i+1;
        }
        string
    }
}

Running forc build yields this error:

13 |         while i < l {
14 |             string::push(i);
   |                     ^^^^ Could not find symbol "push" in this scope.
15 |             i = i+1;
16 |         }
   |

I'm not sure what I'm doing wrong.

Steps to reproduce

  1. Use forc v0.40.1
  2. Create fresh project
  3. Add
    string = { git = "https://github.com/FuelLabs/sway-libs" } as dependency
  4. Write code above in src/main.sw
  5. Profit?

Minimal reproducible code example

contract;

use string::String;

abi MyContract {
    fn return_dynamic_string(l: u8) -> String;
}

impl MyContract for Contract {
    fn return_dynamic_string(l: u8) -> String {
        let mut string = String::new();
        let mut i = 0;
        while i < l {
            string::push(i);
            i = i+1;
        }
        string
    }
}

Rust Version

1.70.0

Forc Version

0.40.1

Expected behavior

I expect the push symbol can be found without adding other imports.

Additional context

No response

This will work:

contract;

use string::String;

abi MyContract {
    fn return_dynamic_string(l: u8) -> String;
}

impl MyContract for Contract {
    fn return_dynamic_string(l: u8) -> String {
        let mut string = String::new();
        let mut i = 0;
        while i < l {
            string.push(i);
            i = i+1;
        }
        string
    }
}

push() is an implementation on self while new() returns Self.

Please note that the String type in Sway-Libs will be removed in favor of FuelLabs/sway#4716 in the std-lib

Whoopsie haha, sorry I got confused reading the readme, closing the issue.