`push` symbol could not be found
Closed this issue · 4 comments
iqdecay commented
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
- Use
forc v0.40.1
- Create fresh project
- Add
string = { git = "https://github.com/FuelLabs/sway-libs" }
as dependency - Write code above in
src/main.sw
- 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
bitzoic commented
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
}
}
bitzoic commented
push()
is an implementation on self while new()
returns Self.
bitzoic commented
Please note that the String
type in Sway-Libs will be removed in favor of FuelLabs/sway#4716 in the std-lib
iqdecay commented
Whoopsie haha, sorry I got confused reading the readme, closing the issue.