这是我的代码,它可以编译
trait AppendBar {
fn append_bar(self) -> Self;
}
impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Self {
self.push("Bar".to_string());
self
}
Run Code Online (Sandbox Code Playgroud)
虽然它获取了所有权,但是为什么它可以在实现过程中改变 self 呢?
let mut a = Box::new("123".to_string());
let b = Box::new( &mut a);
b.push('4');
assert_eq!( "1234", b.as_str());
// lets see the types:
// let x001: Box<&mut Box<String>> = b;
// let x001:&mut Box<String> = *b;
// let x001:Box<String> = **b;
// let x001:String = ***b;
// let x001:str = ****b;
let c = Box::new("456".to_string());
**b = c;
b.push('9');
(*b).push('9');
(**b).push('9');
(***b).push('9');
// (****b).push('9'); // no method named `push` found for type `str` in the current scope
// c.push( 'a'); // cannot mutate …Run Code Online (Sandbox Code Playgroud) 我目前正在学习 rust 中的所有权和借用,以及如何在没有借用的情况下无法使用同一个变量执行函数两次,就像在这个例子中它无法编译一样
enum Computer{
Desktop,
Laptop,
}
fn print_type_of_computer(num: Computer){
match num {
Computer::Desktop => println!("this is a desktop"),
Computer::Laptop => println!("this is a laptop"),
}
}
fn main() {
let myComputer :Computer = Computer::Laptop;
print_type_of_computer(myComputer);
print_type_of_computer(myComputer);
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试这段代码时,即使没有借用,它也能正常工作
fn get_double(num: i32){
println!("{:?}", num * 2);
}
fn main() {
let mynum :i32 = 3;
get_double(mynum);
get_double(mynum);
}
Run Code Online (Sandbox Code Playgroud)
我希望有人可以解释为什么代码在第一种情况下不被接受,但在第二种情况下被接受
我希望我的课程是:
class NumberedString : public Object {
public:
String newName;
short nameID;
NumberedString(String &newName, short nameID) : newName(newName), nameID(nameID) {}
};
HashMap uniqueStrs;//For later.
Run Code Online (Sandbox Code Playgroud)
此方法的实例将传递给HashMap,其继承其堆分配的所有权:
在HashMap.h中(例如):
virtual result Add(const Object& key, const Object& value);
Run Code Online (Sandbox Code Playgroud)
现在,这是我感到困惑的地方。我String在名为的行中分配Add:
uniqueStrs.Add(*(new String(L"XYX_say")), *pNewLoc);
Run Code Online (Sandbox Code Playgroud)
这样HashMap即使我只接受对它的记忆,也可以为我释放此记忆。也许我C在新千年失去了十年,但我认为这是不可能的?
如果不是,那我应该可以写一些类似的东西:
~NumberedString() {
delete &newName;
}
Run Code Online (Sandbox Code Playgroud)
对于我的班级,但除非看到这个库HashMap::RemoveAll()做同样的事情,否则我永远不会猜到。这个问题指出这是不可能的,但只能依靠它auto_ptr,shared_ptr而我的“平台仅支持STL(标准模板库(http://www.sgi.com/tech/stl/)”)。(在整个“标准C ++库”中)。能否所有答案都不要引用此类参考。
谢谢。
评论提示的链接
我无法将链接发布为注释,因此请查看该Add方法及其建议使用的示例:在这里,Benj,String不是std :: string,对不起。
也
我知道它可能会导致尝试删除堆栈对象的崩溃,但是我不知道如何 …
我有问题通过sftp连接到我的ubuntu 14.04服务器.每次我尝试连接时,都会收到以下信息/错误消息:
Sep 18 15:04:47 localhost sshd[2917]: Accepted password for junperbo from 87.129.13.92 port 59333 ssh2
Sep 18 15:04:47 localhost sshd[2917]: pam_unix(sshd:session): session opened for user junperbo by (uid=0)
Sep 18 15:04:47 localhost systemd-logind[2427]: Removed session 2.
Sep 18 15:04:47 localhost systemd-logind[2427]: New session 3 of user junperbo.
Sep 18 15:04:48 localhost sshd[2954]: fatal: bad ownership or modes for chroot directory component "/var/www/"
Sep 18 15:04:48 localhost sshd[2917]: pam_unix(sshd:session): session closed for user junperbo
Run Code Online (Sandbox Code Playgroud)
我是管理我的Ubuntu服务器的新手,所以请详细说明你的答案.我知道问题可以通过"chmod"或/和"chown"来解决,但是怎么样?
请记住,我已经使用此子系统编辑了我的sshd_config:
Subsystem sftp internatl-sftp
Match …Run Code Online (Sandbox Code Playgroud) 我编写了这段代码,以查看将两个字符串传递给函数并将它们再次返回时会发生什么情况:
fn main() {
let mut s3 = String::from("hello");
let mut s4 = String::from("wolrd");
(s3, s4) = take_n_giveback(s3, s4);
println!("{0} and {1}", s3, s4);
}
fn take_n_giveback(x: String, y: String) -> (String, String) {
(x, y)
}
Run Code Online (Sandbox Code Playgroud)
我收到一个没有帮助的错误:
fn main() {
let mut s3 = String::from("hello");
let mut s4 = String::from("wolrd");
(s3, s4) = take_n_giveback(s3, s4);
println!("{0} and {1}", s3, s4);
}
fn take_n_giveback(x: String, y: String) -> (String, String) {
(x, y)
}
Run Code Online (Sandbox Code Playgroud)
传递单个字符串并返回时,此操作工作正常。
fn main() {
let mut …Run Code Online (Sandbox Code Playgroud) ownership ×6
rust ×4
mutability ×2
bada ×1
c++ ×1
immutability ×1
memory-leaks ×1
sftp ×1
traits ×1
ubuntu ×1