Passing a string slice, returning a string slice. But who owns it?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm a beginner to Rust and just learning the ownership concept.
I'm using this function to reverse a String
fn reverse(input: &str) -> String {
//we are receiving a borrowed value,
input
//get an iterator of chars from the string slice
.chars()
//Goes between two iterators.
//From the doc: double-ended iterator with the direction inverted.
.rev()
//collect into a String
.collect::<String>()
}
fn process_reverse_case(input: &str, expected: &str) {
assert_eq!(&reverse(input), expected)
}
fn main() {
process_reverse_case("robot", "tobor");
}
I would like to understand who owns robot and tobor.
- I'm passing a borrowed value. Which is a string slice.
- I understand this can't be modified. So when we are collecting the reversed string, I think we are collecting it into a
argument 1ofassert_eq!. Am I right? - But as the process of reversing + collection happens inside
reverse, the memory needed keeps increasing. Doesargument 1ofassert_eq!account for that? - The above question might have been solved by the compiler at compile time. Am I right?
rust
add a comment |
I'm a beginner to Rust and just learning the ownership concept.
I'm using this function to reverse a String
fn reverse(input: &str) -> String {
//we are receiving a borrowed value,
input
//get an iterator of chars from the string slice
.chars()
//Goes between two iterators.
//From the doc: double-ended iterator with the direction inverted.
.rev()
//collect into a String
.collect::<String>()
}
fn process_reverse_case(input: &str, expected: &str) {
assert_eq!(&reverse(input), expected)
}
fn main() {
process_reverse_case("robot", "tobor");
}
I would like to understand who owns robot and tobor.
- I'm passing a borrowed value. Which is a string slice.
- I understand this can't be modified. So when we are collecting the reversed string, I think we are collecting it into a
argument 1ofassert_eq!. Am I right? - But as the process of reversing + collection happens inside
reverse, the memory needed keeps increasing. Doesargument 1ofassert_eq!account for that? - The above question might have been solved by the compiler at compile time. Am I right?
rust
add a comment |
I'm a beginner to Rust and just learning the ownership concept.
I'm using this function to reverse a String
fn reverse(input: &str) -> String {
//we are receiving a borrowed value,
input
//get an iterator of chars from the string slice
.chars()
//Goes between two iterators.
//From the doc: double-ended iterator with the direction inverted.
.rev()
//collect into a String
.collect::<String>()
}
fn process_reverse_case(input: &str, expected: &str) {
assert_eq!(&reverse(input), expected)
}
fn main() {
process_reverse_case("robot", "tobor");
}
I would like to understand who owns robot and tobor.
- I'm passing a borrowed value. Which is a string slice.
- I understand this can't be modified. So when we are collecting the reversed string, I think we are collecting it into a
argument 1ofassert_eq!. Am I right? - But as the process of reversing + collection happens inside
reverse, the memory needed keeps increasing. Doesargument 1ofassert_eq!account for that? - The above question might have been solved by the compiler at compile time. Am I right?
rust
I'm a beginner to Rust and just learning the ownership concept.
I'm using this function to reverse a String
fn reverse(input: &str) -> String {
//we are receiving a borrowed value,
input
//get an iterator of chars from the string slice
.chars()
//Goes between two iterators.
//From the doc: double-ended iterator with the direction inverted.
.rev()
//collect into a String
.collect::<String>()
}
fn process_reverse_case(input: &str, expected: &str) {
assert_eq!(&reverse(input), expected)
}
fn main() {
process_reverse_case("robot", "tobor");
}
I would like to understand who owns robot and tobor.
- I'm passing a borrowed value. Which is a string slice.
- I understand this can't be modified. So when we are collecting the reversed string, I think we are collecting it into a
argument 1ofassert_eq!. Am I right? - But as the process of reversing + collection happens inside
reverse, the memory needed keeps increasing. Doesargument 1ofassert_eq!account for that? - The above question might have been solved by the compiler at compile time. Am I right?
rust
rust
edited Nov 23 '18 at 11:11
clmno
asked Nov 23 '18 at 10:42
clmnoclmno
734416
734416
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Your claims are mostly wrong, let me try to correct them.
Passing a string slice, returning a string slice
fn reverse(input: &str) -> String you are accepting a string slice, but returning a String. A String "has ownership over the contents of the string".
I understand this [a string slice] can't be modified
You can modify a &mut str, e.g.
fn to_lower(s: &mut str) {
s.make_ascii_lowercase();
}
fn main() {
let mut a = String::from("ABC");
println!("{}", a);
to_lower(&mut a);
println!("{}", a);
}
(playground)
So when we are collecting the reversed string, I think we are collecting it into a argument 1 of assert_eq!
No. You collect it into a string, that is what collect::<String>() is for. Also your function reverse returns a String.
This means, that you are calling the function reverse, which returns a String and you are passing that String as the first "argument" to the assert_eq! macro.
the memory needed keeps increasing. Does argument 1 of assert_eq! account for that?
No, how should it? That's all done inside of the reverse function. assert_eq! just takes two parameters. Where they come from is uncertain and not needed to know.
assert_eq!(&reverse(input), expected)
What happens in this line is, that you are calling your reverse function with input as parameter. But normally you can't compare &T with T (a reference to something with an actual instance of something), that's where the & (ampersand) in front of reverse comes into play. String implements the Deref trait which means it can be seen as a str as well (see the doc for Deref). But now you are trying to compare a str with &str, which does not work and that's why you put & in front, so you actually end up with &str and &str which you can compare with ==.
I would like to understand who ownes
robotandtobor.
Nobody. In fact they live inside of the data section in the ELF/Binary itself. They don't have an owner per se, but can be used as &str wherever needed.
add a 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%2f53445132%2fpassing-a-string-slice-returning-a-string-slice-but-who-owns-it%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
Your claims are mostly wrong, let me try to correct them.
Passing a string slice, returning a string slice
fn reverse(input: &str) -> String you are accepting a string slice, but returning a String. A String "has ownership over the contents of the string".
I understand this [a string slice] can't be modified
You can modify a &mut str, e.g.
fn to_lower(s: &mut str) {
s.make_ascii_lowercase();
}
fn main() {
let mut a = String::from("ABC");
println!("{}", a);
to_lower(&mut a);
println!("{}", a);
}
(playground)
So when we are collecting the reversed string, I think we are collecting it into a argument 1 of assert_eq!
No. You collect it into a string, that is what collect::<String>() is for. Also your function reverse returns a String.
This means, that you are calling the function reverse, which returns a String and you are passing that String as the first "argument" to the assert_eq! macro.
the memory needed keeps increasing. Does argument 1 of assert_eq! account for that?
No, how should it? That's all done inside of the reverse function. assert_eq! just takes two parameters. Where they come from is uncertain and not needed to know.
assert_eq!(&reverse(input), expected)
What happens in this line is, that you are calling your reverse function with input as parameter. But normally you can't compare &T with T (a reference to something with an actual instance of something), that's where the & (ampersand) in front of reverse comes into play. String implements the Deref trait which means it can be seen as a str as well (see the doc for Deref). But now you are trying to compare a str with &str, which does not work and that's why you put & in front, so you actually end up with &str and &str which you can compare with ==.
I would like to understand who ownes
robotandtobor.
Nobody. In fact they live inside of the data section in the ELF/Binary itself. They don't have an owner per se, but can be used as &str wherever needed.
add a comment |
Your claims are mostly wrong, let me try to correct them.
Passing a string slice, returning a string slice
fn reverse(input: &str) -> String you are accepting a string slice, but returning a String. A String "has ownership over the contents of the string".
I understand this [a string slice] can't be modified
You can modify a &mut str, e.g.
fn to_lower(s: &mut str) {
s.make_ascii_lowercase();
}
fn main() {
let mut a = String::from("ABC");
println!("{}", a);
to_lower(&mut a);
println!("{}", a);
}
(playground)
So when we are collecting the reversed string, I think we are collecting it into a argument 1 of assert_eq!
No. You collect it into a string, that is what collect::<String>() is for. Also your function reverse returns a String.
This means, that you are calling the function reverse, which returns a String and you are passing that String as the first "argument" to the assert_eq! macro.
the memory needed keeps increasing. Does argument 1 of assert_eq! account for that?
No, how should it? That's all done inside of the reverse function. assert_eq! just takes two parameters. Where they come from is uncertain and not needed to know.
assert_eq!(&reverse(input), expected)
What happens in this line is, that you are calling your reverse function with input as parameter. But normally you can't compare &T with T (a reference to something with an actual instance of something), that's where the & (ampersand) in front of reverse comes into play. String implements the Deref trait which means it can be seen as a str as well (see the doc for Deref). But now you are trying to compare a str with &str, which does not work and that's why you put & in front, so you actually end up with &str and &str which you can compare with ==.
I would like to understand who ownes
robotandtobor.
Nobody. In fact they live inside of the data section in the ELF/Binary itself. They don't have an owner per se, but can be used as &str wherever needed.
add a comment |
Your claims are mostly wrong, let me try to correct them.
Passing a string slice, returning a string slice
fn reverse(input: &str) -> String you are accepting a string slice, but returning a String. A String "has ownership over the contents of the string".
I understand this [a string slice] can't be modified
You can modify a &mut str, e.g.
fn to_lower(s: &mut str) {
s.make_ascii_lowercase();
}
fn main() {
let mut a = String::from("ABC");
println!("{}", a);
to_lower(&mut a);
println!("{}", a);
}
(playground)
So when we are collecting the reversed string, I think we are collecting it into a argument 1 of assert_eq!
No. You collect it into a string, that is what collect::<String>() is for. Also your function reverse returns a String.
This means, that you are calling the function reverse, which returns a String and you are passing that String as the first "argument" to the assert_eq! macro.
the memory needed keeps increasing. Does argument 1 of assert_eq! account for that?
No, how should it? That's all done inside of the reverse function. assert_eq! just takes two parameters. Where they come from is uncertain and not needed to know.
assert_eq!(&reverse(input), expected)
What happens in this line is, that you are calling your reverse function with input as parameter. But normally you can't compare &T with T (a reference to something with an actual instance of something), that's where the & (ampersand) in front of reverse comes into play. String implements the Deref trait which means it can be seen as a str as well (see the doc for Deref). But now you are trying to compare a str with &str, which does not work and that's why you put & in front, so you actually end up with &str and &str which you can compare with ==.
I would like to understand who ownes
robotandtobor.
Nobody. In fact they live inside of the data section in the ELF/Binary itself. They don't have an owner per se, but can be used as &str wherever needed.
Your claims are mostly wrong, let me try to correct them.
Passing a string slice, returning a string slice
fn reverse(input: &str) -> String you are accepting a string slice, but returning a String. A String "has ownership over the contents of the string".
I understand this [a string slice] can't be modified
You can modify a &mut str, e.g.
fn to_lower(s: &mut str) {
s.make_ascii_lowercase();
}
fn main() {
let mut a = String::from("ABC");
println!("{}", a);
to_lower(&mut a);
println!("{}", a);
}
(playground)
So when we are collecting the reversed string, I think we are collecting it into a argument 1 of assert_eq!
No. You collect it into a string, that is what collect::<String>() is for. Also your function reverse returns a String.
This means, that you are calling the function reverse, which returns a String and you are passing that String as the first "argument" to the assert_eq! macro.
the memory needed keeps increasing. Does argument 1 of assert_eq! account for that?
No, how should it? That's all done inside of the reverse function. assert_eq! just takes two parameters. Where they come from is uncertain and not needed to know.
assert_eq!(&reverse(input), expected)
What happens in this line is, that you are calling your reverse function with input as parameter. But normally you can't compare &T with T (a reference to something with an actual instance of something), that's where the & (ampersand) in front of reverse comes into play. String implements the Deref trait which means it can be seen as a str as well (see the doc for Deref). But now you are trying to compare a str with &str, which does not work and that's why you put & in front, so you actually end up with &str and &str which you can compare with ==.
I would like to understand who ownes
robotandtobor.
Nobody. In fact they live inside of the data section in the ELF/Binary itself. They don't have an owner per se, but can be used as &str wherever needed.
edited Nov 23 '18 at 11:32
answered Nov 23 '18 at 11:04
hellowhellow
5,44952344
5,44952344
add a comment |
add a 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%2f53445132%2fpassing-a-string-slice-returning-a-string-slice-but-who-owns-it%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