Why do I get “use of undeclared type or module” error when calling a struct's function?Use of undeclared type or module `std` when used in a separate moduleUse of undeclared type or module core::fmt::DisplayWhy is the `std` module undeclared?Embedding a borrowed value into an enum?Implementing Nested TraitsEncapsulating sequentially initialized state with self-references in Rust structError E0433 - Use of undeclared type or module when accessing a module from a testHow to provide type-only argument to a function?How to create trait object from boxed trait?Store data that implements a trait in a vector
How to befriend someone who doesn't like to talk?
Is it advisable to add a location heads-up when a scene changes in a novel?
Make Gimbap cutter
Grandpa has another non math question
What's the difference between DHCP and NAT? Are they mutually exclusive?
Course development: can I pay someone to make slides for the course?
How to show a "node near coord" even when it is out of bounds (with clip = true)?
What is this wall covering type?
How does AFV select the winning videos?
Forgot passport for Alaska cruise (Anchorage to Vancouver)
How do I type a hyphen in iOS 12?
What is the theme of analysis?
Entered UK using my now-lost UK passport; can I go to Spain using my US passport?
Should I list a completely different profession in my technical resume?
ASCII Meme Arrow Generator
Can I use 220 V outlets on a 15 ampere breaker and wire it up as 110 V?
What is the STRONGEST end-of-line knot to use if you want to use a steel-thimble at the end, so that you've got a steel-eyelet at the end of the line?
How to generate list of *all* available commands and functions?
Why is long-term living in Almost-Earth causing severe health problems?
Why are ambiguous grammars bad?
Why does there seem to be an extreme lack of public trashcans in Taiwan?
Do Veracrypt encrypted volumes have any kind of brute force protection?
What class is best to play when a level behind the rest of the party?
What is the proper event in Extended Events to track stored procedure executions?
Why do I get “use of undeclared type or module” error when calling a struct's function?
Use of undeclared type or module `std` when used in a separate moduleUse of undeclared type or module core::fmt::DisplayWhy is the `std` module undeclared?Embedding a borrowed value into an enum?Implementing Nested TraitsEncapsulating sequentially initialized state with self-references in Rust structError E0433 - Use of undeclared type or module when accessing a module from a testHow to provide type-only argument to a function?How to create trait object from boxed trait?Store data that implements a trait in a vector
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have the following code in Rust:
trait MyTrait
fn get_value() -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value() -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct::get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt)
This code is fine. It defines a trait and a struct. The struct implements the trait; which requires to implement a function. Everything is fine until now. But if I try the following code:
trait MyTrait
fn get_value() -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value() -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct::get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt::get_value())
I get the following error:
error[E0433]: failed to resolve: use of undeclared type or module `trt`
--> src/main.rs:21:22
|
21 | println!(":?", trt::get_value())
| ^^^ use of undeclared type or module `trt`
Now, I don't really understand very well why that wouldn't work. trt
should represent a copy of myStruct
and then it should have its own functions, right?
Interestingly, this following code will compile:
trait MyTrait
fn get_value(&self) -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value(&self) -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct.get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt.get_value())
So what is exactly wrong with the code that doesn't compile?
rust
add a comment |
I have the following code in Rust:
trait MyTrait
fn get_value() -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value() -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct::get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt)
This code is fine. It defines a trait and a struct. The struct implements the trait; which requires to implement a function. Everything is fine until now. But if I try the following code:
trait MyTrait
fn get_value() -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value() -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct::get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt::get_value())
I get the following error:
error[E0433]: failed to resolve: use of undeclared type or module `trt`
--> src/main.rs:21:22
|
21 | println!(":?", trt::get_value())
| ^^^ use of undeclared type or module `trt`
Now, I don't really understand very well why that wouldn't work. trt
should represent a copy of myStruct
and then it should have its own functions, right?
Interestingly, this following code will compile:
trait MyTrait
fn get_value(&self) -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value(&self) -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct.get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt.get_value())
So what is exactly wrong with the code that doesn't compile?
rust
add a comment |
I have the following code in Rust:
trait MyTrait
fn get_value() -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value() -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct::get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt)
This code is fine. It defines a trait and a struct. The struct implements the trait; which requires to implement a function. Everything is fine until now. But if I try the following code:
trait MyTrait
fn get_value() -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value() -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct::get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt::get_value())
I get the following error:
error[E0433]: failed to resolve: use of undeclared type or module `trt`
--> src/main.rs:21:22
|
21 | println!(":?", trt::get_value())
| ^^^ use of undeclared type or module `trt`
Now, I don't really understand very well why that wouldn't work. trt
should represent a copy of myStruct
and then it should have its own functions, right?
Interestingly, this following code will compile:
trait MyTrait
fn get_value(&self) -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value(&self) -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct.get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt.get_value())
So what is exactly wrong with the code that doesn't compile?
rust
I have the following code in Rust:
trait MyTrait
fn get_value() -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value() -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct::get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt)
This code is fine. It defines a trait and a struct. The struct implements the trait; which requires to implement a function. Everything is fine until now. But if I try the following code:
trait MyTrait
fn get_value() -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value() -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct::get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt::get_value())
I get the following error:
error[E0433]: failed to resolve: use of undeclared type or module `trt`
--> src/main.rs:21:22
|
21 | println!(":?", trt::get_value())
| ^^^ use of undeclared type or module `trt`
Now, I don't really understand very well why that wouldn't work. trt
should represent a copy of myStruct
and then it should have its own functions, right?
Interestingly, this following code will compile:
trait MyTrait
fn get_value(&self) -> &'static str;
#[derive(Debug)]
struct MyStruct;
impl MyTrait for MyStruct
fn get_value(&self) -> &'static str
"has value"
fn main()
println!("My value: ", MyStruct.get_value());
has_trait(MyStruct);
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", trt.get_value())
So what is exactly wrong with the code that doesn't compile?
rust
rust
edited Mar 24 at 23:27
Omar Abid
asked Mar 24 at 22:57
Omar AbidOmar Abid
7,136206896
7,136206896
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Now, I don't really understand very well why that wouldn't work.
trt
should represent a copy ofMyStruct
and then it should have its own functions, right?
It doesn't quite work that way for associated functions in Rust.
With the identifier trt
, you can call methods where trt
is the receiver (self
or one of its variations such as &self
or &mut self
). However, get_value()
does not have a receiver, so it is an associated function. This resembles a static method in some languages such as Java. Unlike Java, associated functions in Rust can only be called by specifying the type or type parameter with that function:
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", T::get_value())
This will now work, and would not even need the parameter trt
, because we're just calling an associated function of the type T
, rather than a method.
Although trt
is an identifier to a function parameter in this context, the compiler will actually try to interpret it as something else (module name, type name, ...) once combined with the ::
token, hence the given error message.
Thanks. It works!. However, doesn't T represent the Trait. The trait doesn't implement the function. The struct does (which is passed through the function). Trying to display it, it says "expected value, found type parameter T". To be honest, my brain still doesn't understand it.
– Omar Abid
Mar 24 at 23:36
@OmarAbid Traits declare things, rather than implement themselves.MyTrait
does in fact declare the associated functionget_value()
, which is why it can be called from a typeT
bound by that trait. That other error must have emerged from an attempt to use the type as a value.
– Sir E_net4 the Downvoter
Mar 24 at 23:41
I think it is starting to make some sense. T is myStruct. But isn't "trt" myStruct too? In that case, what is the difference between trt and T?
– Omar Abid
Mar 24 at 23:53
1
T
is a type parameter of the functionhas_trait
. When this function is called, thisT
is monomorphized to a concrete type, in this caseMyStruct
.trt
is a value of typeT
, and so, will be a value of typeMyStruct
whenT
isMyStruct
. One is a type, whereas the other one is an actual value. It might be worth revisiting section 10.2 of the book, which contains a deeper explanation about traits and monomorphization.
– Sir E_net4 the Downvoter
Mar 24 at 23:57
2
@OmarAbidMyStruct
is a type.trt
is a value which happens to have typeMyStruct
. You can call a method on a value using the.
operator. You can call an associated function on a type using the::
operator. But you can't use the.
operator on a type and you can't use the::
operator on a value. The difference between values and types is very stark in Rust; it's a good thing to have a handle on.
– trentcl
Mar 25 at 0:07
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329386%2fwhy-do-i-get-use-of-undeclared-type-or-module-error-when-calling-a-structs-fu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Now, I don't really understand very well why that wouldn't work.
trt
should represent a copy ofMyStruct
and then it should have its own functions, right?
It doesn't quite work that way for associated functions in Rust.
With the identifier trt
, you can call methods where trt
is the receiver (self
or one of its variations such as &self
or &mut self
). However, get_value()
does not have a receiver, so it is an associated function. This resembles a static method in some languages such as Java. Unlike Java, associated functions in Rust can only be called by specifying the type or type parameter with that function:
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", T::get_value())
This will now work, and would not even need the parameter trt
, because we're just calling an associated function of the type T
, rather than a method.
Although trt
is an identifier to a function parameter in this context, the compiler will actually try to interpret it as something else (module name, type name, ...) once combined with the ::
token, hence the given error message.
Thanks. It works!. However, doesn't T represent the Trait. The trait doesn't implement the function. The struct does (which is passed through the function). Trying to display it, it says "expected value, found type parameter T". To be honest, my brain still doesn't understand it.
– Omar Abid
Mar 24 at 23:36
@OmarAbid Traits declare things, rather than implement themselves.MyTrait
does in fact declare the associated functionget_value()
, which is why it can be called from a typeT
bound by that trait. That other error must have emerged from an attempt to use the type as a value.
– Sir E_net4 the Downvoter
Mar 24 at 23:41
I think it is starting to make some sense. T is myStruct. But isn't "trt" myStruct too? In that case, what is the difference between trt and T?
– Omar Abid
Mar 24 at 23:53
1
T
is a type parameter of the functionhas_trait
. When this function is called, thisT
is monomorphized to a concrete type, in this caseMyStruct
.trt
is a value of typeT
, and so, will be a value of typeMyStruct
whenT
isMyStruct
. One is a type, whereas the other one is an actual value. It might be worth revisiting section 10.2 of the book, which contains a deeper explanation about traits and monomorphization.
– Sir E_net4 the Downvoter
Mar 24 at 23:57
2
@OmarAbidMyStruct
is a type.trt
is a value which happens to have typeMyStruct
. You can call a method on a value using the.
operator. You can call an associated function on a type using the::
operator. But you can't use the.
operator on a type and you can't use the::
operator on a value. The difference between values and types is very stark in Rust; it's a good thing to have a handle on.
– trentcl
Mar 25 at 0:07
|
show 1 more comment
Now, I don't really understand very well why that wouldn't work.
trt
should represent a copy ofMyStruct
and then it should have its own functions, right?
It doesn't quite work that way for associated functions in Rust.
With the identifier trt
, you can call methods where trt
is the receiver (self
or one of its variations such as &self
or &mut self
). However, get_value()
does not have a receiver, so it is an associated function. This resembles a static method in some languages such as Java. Unlike Java, associated functions in Rust can only be called by specifying the type or type parameter with that function:
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", T::get_value())
This will now work, and would not even need the parameter trt
, because we're just calling an associated function of the type T
, rather than a method.
Although trt
is an identifier to a function parameter in this context, the compiler will actually try to interpret it as something else (module name, type name, ...) once combined with the ::
token, hence the given error message.
Thanks. It works!. However, doesn't T represent the Trait. The trait doesn't implement the function. The struct does (which is passed through the function). Trying to display it, it says "expected value, found type parameter T". To be honest, my brain still doesn't understand it.
– Omar Abid
Mar 24 at 23:36
@OmarAbid Traits declare things, rather than implement themselves.MyTrait
does in fact declare the associated functionget_value()
, which is why it can be called from a typeT
bound by that trait. That other error must have emerged from an attempt to use the type as a value.
– Sir E_net4 the Downvoter
Mar 24 at 23:41
I think it is starting to make some sense. T is myStruct. But isn't "trt" myStruct too? In that case, what is the difference between trt and T?
– Omar Abid
Mar 24 at 23:53
1
T
is a type parameter of the functionhas_trait
. When this function is called, thisT
is monomorphized to a concrete type, in this caseMyStruct
.trt
is a value of typeT
, and so, will be a value of typeMyStruct
whenT
isMyStruct
. One is a type, whereas the other one is an actual value. It might be worth revisiting section 10.2 of the book, which contains a deeper explanation about traits and monomorphization.
– Sir E_net4 the Downvoter
Mar 24 at 23:57
2
@OmarAbidMyStruct
is a type.trt
is a value which happens to have typeMyStruct
. You can call a method on a value using the.
operator. You can call an associated function on a type using the::
operator. But you can't use the.
operator on a type and you can't use the::
operator on a value. The difference between values and types is very stark in Rust; it's a good thing to have a handle on.
– trentcl
Mar 25 at 0:07
|
show 1 more comment
Now, I don't really understand very well why that wouldn't work.
trt
should represent a copy ofMyStruct
and then it should have its own functions, right?
It doesn't quite work that way for associated functions in Rust.
With the identifier trt
, you can call methods where trt
is the receiver (self
or one of its variations such as &self
or &mut self
). However, get_value()
does not have a receiver, so it is an associated function. This resembles a static method in some languages such as Java. Unlike Java, associated functions in Rust can only be called by specifying the type or type parameter with that function:
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", T::get_value())
This will now work, and would not even need the parameter trt
, because we're just calling an associated function of the type T
, rather than a method.
Although trt
is an identifier to a function parameter in this context, the compiler will actually try to interpret it as something else (module name, type name, ...) once combined with the ::
token, hence the given error message.
Now, I don't really understand very well why that wouldn't work.
trt
should represent a copy ofMyStruct
and then it should have its own functions, right?
It doesn't quite work that way for associated functions in Rust.
With the identifier trt
, you can call methods where trt
is the receiver (self
or one of its variations such as &self
or &mut self
). However, get_value()
does not have a receiver, so it is an associated function. This resembles a static method in some languages such as Java. Unlike Java, associated functions in Rust can only be called by specifying the type or type parameter with that function:
fn has_trait<T>(trt: T) where T: MyTrait + std::fmt::Debug
println!(":?", T::get_value())
This will now work, and would not even need the parameter trt
, because we're just calling an associated function of the type T
, rather than a method.
Although trt
is an identifier to a function parameter in this context, the compiler will actually try to interpret it as something else (module name, type name, ...) once combined with the ::
token, hence the given error message.
edited Mar 24 at 23:28
answered Mar 24 at 23:18
Sir E_net4 the DownvoterSir E_net4 the Downvoter
13.7k74077
13.7k74077
Thanks. It works!. However, doesn't T represent the Trait. The trait doesn't implement the function. The struct does (which is passed through the function). Trying to display it, it says "expected value, found type parameter T". To be honest, my brain still doesn't understand it.
– Omar Abid
Mar 24 at 23:36
@OmarAbid Traits declare things, rather than implement themselves.MyTrait
does in fact declare the associated functionget_value()
, which is why it can be called from a typeT
bound by that trait. That other error must have emerged from an attempt to use the type as a value.
– Sir E_net4 the Downvoter
Mar 24 at 23:41
I think it is starting to make some sense. T is myStruct. But isn't "trt" myStruct too? In that case, what is the difference between trt and T?
– Omar Abid
Mar 24 at 23:53
1
T
is a type parameter of the functionhas_trait
. When this function is called, thisT
is monomorphized to a concrete type, in this caseMyStruct
.trt
is a value of typeT
, and so, will be a value of typeMyStruct
whenT
isMyStruct
. One is a type, whereas the other one is an actual value. It might be worth revisiting section 10.2 of the book, which contains a deeper explanation about traits and monomorphization.
– Sir E_net4 the Downvoter
Mar 24 at 23:57
2
@OmarAbidMyStruct
is a type.trt
is a value which happens to have typeMyStruct
. You can call a method on a value using the.
operator. You can call an associated function on a type using the::
operator. But you can't use the.
operator on a type and you can't use the::
operator on a value. The difference between values and types is very stark in Rust; it's a good thing to have a handle on.
– trentcl
Mar 25 at 0:07
|
show 1 more comment
Thanks. It works!. However, doesn't T represent the Trait. The trait doesn't implement the function. The struct does (which is passed through the function). Trying to display it, it says "expected value, found type parameter T". To be honest, my brain still doesn't understand it.
– Omar Abid
Mar 24 at 23:36
@OmarAbid Traits declare things, rather than implement themselves.MyTrait
does in fact declare the associated functionget_value()
, which is why it can be called from a typeT
bound by that trait. That other error must have emerged from an attempt to use the type as a value.
– Sir E_net4 the Downvoter
Mar 24 at 23:41
I think it is starting to make some sense. T is myStruct. But isn't "trt" myStruct too? In that case, what is the difference between trt and T?
– Omar Abid
Mar 24 at 23:53
1
T
is a type parameter of the functionhas_trait
. When this function is called, thisT
is monomorphized to a concrete type, in this caseMyStruct
.trt
is a value of typeT
, and so, will be a value of typeMyStruct
whenT
isMyStruct
. One is a type, whereas the other one is an actual value. It might be worth revisiting section 10.2 of the book, which contains a deeper explanation about traits and monomorphization.
– Sir E_net4 the Downvoter
Mar 24 at 23:57
2
@OmarAbidMyStruct
is a type.trt
is a value which happens to have typeMyStruct
. You can call a method on a value using the.
operator. You can call an associated function on a type using the::
operator. But you can't use the.
operator on a type and you can't use the::
operator on a value. The difference between values and types is very stark in Rust; it's a good thing to have a handle on.
– trentcl
Mar 25 at 0:07
Thanks. It works!. However, doesn't T represent the Trait. The trait doesn't implement the function. The struct does (which is passed through the function). Trying to display it, it says "expected value, found type parameter T". To be honest, my brain still doesn't understand it.
– Omar Abid
Mar 24 at 23:36
Thanks. It works!. However, doesn't T represent the Trait. The trait doesn't implement the function. The struct does (which is passed through the function). Trying to display it, it says "expected value, found type parameter T". To be honest, my brain still doesn't understand it.
– Omar Abid
Mar 24 at 23:36
@OmarAbid Traits declare things, rather than implement themselves.
MyTrait
does in fact declare the associated function get_value()
, which is why it can be called from a type T
bound by that trait. That other error must have emerged from an attempt to use the type as a value.– Sir E_net4 the Downvoter
Mar 24 at 23:41
@OmarAbid Traits declare things, rather than implement themselves.
MyTrait
does in fact declare the associated function get_value()
, which is why it can be called from a type T
bound by that trait. That other error must have emerged from an attempt to use the type as a value.– Sir E_net4 the Downvoter
Mar 24 at 23:41
I think it is starting to make some sense. T is myStruct. But isn't "trt" myStruct too? In that case, what is the difference between trt and T?
– Omar Abid
Mar 24 at 23:53
I think it is starting to make some sense. T is myStruct. But isn't "trt" myStruct too? In that case, what is the difference between trt and T?
– Omar Abid
Mar 24 at 23:53
1
1
T
is a type parameter of the function has_trait
. When this function is called, this T
is monomorphized to a concrete type, in this case MyStruct
. trt
is a value of type T
, and so, will be a value of type MyStruct
when T
is MyStruct
. One is a type, whereas the other one is an actual value. It might be worth revisiting section 10.2 of the book, which contains a deeper explanation about traits and monomorphization.– Sir E_net4 the Downvoter
Mar 24 at 23:57
T
is a type parameter of the function has_trait
. When this function is called, this T
is monomorphized to a concrete type, in this case MyStruct
. trt
is a value of type T
, and so, will be a value of type MyStruct
when T
is MyStruct
. One is a type, whereas the other one is an actual value. It might be worth revisiting section 10.2 of the book, which contains a deeper explanation about traits and monomorphization.– Sir E_net4 the Downvoter
Mar 24 at 23:57
2
2
@OmarAbid
MyStruct
is a type. trt
is a value which happens to have type MyStruct
. You can call a method on a value using the .
operator. You can call an associated function on a type using the ::
operator. But you can't use the .
operator on a type and you can't use the ::
operator on a value. The difference between values and types is very stark in Rust; it's a good thing to have a handle on.– trentcl
Mar 25 at 0:07
@OmarAbid
MyStruct
is a type. trt
is a value which happens to have type MyStruct
. You can call a method on a value using the .
operator. You can call an associated function on a type using the ::
operator. But you can't use the .
operator on a type and you can't use the ::
operator on a value. The difference between values and types is very stark in Rust; it's a good thing to have a handle on.– trentcl
Mar 25 at 0:07
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55329386%2fwhy-do-i-get-use-of-undeclared-type-or-module-error-when-calling-a-structs-fu%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown