-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Closed
Description
Hello,
It took me some time to understand the following problem.
The following code ends with error:
error: the type of this value must be known in this context
--> main.rs:14:34
|
14 | println!("Hello, world! {}", sb.flag);
| ^^^^^^^
use std::rc::Rc;
use std::cell::RefCell;
use std::borrow::Borrow;
pub struct S {
flag : bool
}
type SCell = Rc<RefCell<S>>;
fn test(s : SCell) {
let sb = &s.borrow();
println!("Hello, world! {}", sb.flag);
}
fn main() {
let s = Rc::new(RefCell::new(S{flag: false}));
test(s);
}However, the similar construction works in many other situation and a natural fix (let sb : &S = &s.borrow();) does not work. I was not able to understand the problem until @gavento pointed out that the problem is with use std::borrow::Borrow;. When the line is removed, the code is compiled without any error.
I suggest to improve the error message in a way that indicates that problem is with the borrow method, since the current error message pointing to the structure usage is quite confusing (at least for me:).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Fields
Give feedbackNo fields configured for issues without a type.